[SQL]If user is not found?

Experienced Elementalist
Joined
Apr 15, 2008
Messages
256
Reaction score
0
Hi im using this script :
PHP:
<?php
include("includes/connect.php");
if(isset($_GET['user'])){
$rs=mysql_query("SELECT * FROM users WHERE username='".$_GET['user']."'")or die("Blog not found");
while($d=mysql_fetch_assoc($rs)){
echo "Welcome to ".$d['username']."'s Blog!";
} 
} ?>
How would you make it so that is the user is not found ex:
user.php?user=LKJELj2l3kj2l3kj
it would echo something?
Thanks
 
PHP:
if(isset($_GET['user']))
{
$query = mysql_query("SELECT * FROM users WHERE username='".$_GET['user']."'")or die("Blog not found"); 
$res = @mysql_fetch_array($query);
if(count($res) < 1) die('No such user');
echo "Welcome to ".$res['username']."'s Blog!"; 
}
 
PHP:
 <?php
include("includes/connect.php");
if(isset($_GET['user'])){
$rs=mysql_query("SELECT * FROM users WHERE username='".$_GET['user']."'")or die("Blog not found");
while($d=mysql_fetch_assoc($rs)){
echo "Welcome to ".$d['username']."'s Blog!";
} 
else{ 
echo "Username not found."; 
} 
?>

mines probably wrong but its what i use on my DMS
 
Hi im using this script :
PHP:
<?php
include("includes/connect.php");
if(isset($_GET['user'])){
$rs=mysql_query("SELECT * FROM users WHERE username='".$_GET['user']."'")or die("Blog not found");
while($d=mysql_fetch_assoc($rs)){
echo "Welcome to ".$d['username']."'s Blog!";
} 
} ?>
How would you make it so that is the user is not found ex:
user.php?user=LKJELj2l3kj2l3kj
it would echo something?
Thanks

Well for starters, why are you using a while loop if there will only be one row? Also, why are you using the string concatenation operator when you are using double quotes? Plus, indentation couldn't hurt.

PHP:
<?php
include("includes/connect.php");
if (isset($_GET['user'])){
    $rs  = mysql_query("SELECT * FROM users WHERE username='".$_GET['user']."'")or die("Blog not found");
    $user = mysql_fetch_assoc($rs);
    
    if ($user !== FALSE) {
       echo "Welcome to {$user['username']}'s Blog!";
    }
    else {
        echo 'Whatever';
    }
} ?>

Honestly, write better code.
 
Well for starters, why are you using a while loop if there will only be one row? Also, why are you using the string concatenation operator when you are using double quotes? Plus, indentation couldn't hurt.

PHP:
<?php
include("includes/connect.php");
if (isset($_GET['user'])){
    $rs  = mysql_query("SELECT * FROM users WHERE username='".$_GET['user']."'")or die("Blog not found");
    $user = mysql_fetch_assoc($rs);
    
    if ($user !== FALSE) {
       echo "Welcome to {$user['username']}'s Blog!";
    }
    else {
        echo 'Whatever';
    }
} ?>

Honestly, write better code.
Just starting it off, could of made it post their age or anything
 
Just starting it off, could of made it post their age or anything
loop would be required if you'd want to print like all users, but for only one, it's senseless.

anyway this is how I'm doing it(bit messy because of redoing few things after applying mod_rewrite..)
PHP:
//get id from that user with username from get, because of fucking mod_rewrite
$name             = $_GET['u'];
$query            = "SELECT * FROM user WHERE username = '$name'";
$result           = mysql_query($query);
$array            = mysql_fetch_array($result);

//get everything from users where id is $name's id
$id             = $array['id'];
$timesviewed    = views_user($id);
$query = "
            SELECT
                user.*,
                userdetails.*
            FROM
                user,userdetails
            WHERE
                userdetails.userID = '$id'
            AND 
                user.ID = '$id'
        ";
$result = mysql_query($query);
$array  = mysql_fetch_array($result);

//check if id from $_GET contains a profile lol
if( $array['userID'] == NULL )
{    
    index_redir();
    echo "This profile doesn't exist, redirecting to the main page";
}
//if it does exist..
else
{
/*
********** 
print all user's details
**********
*/
}
 
Last edited:
Back