Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[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
 
Experienced Elementalist
Joined
Apr 7, 2005
Messages
254
Reaction score
0
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!"; 
}
 
Custom Title Activated
Loyal Member
Joined
Mar 9, 2004
Messages
2,425
Reaction score
2
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
 
Experienced Elementalist
Joined
Dec 27, 2006
Messages
288
Reaction score
4
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.
 
Experienced Elementalist
Joined
Apr 15, 2008
Messages
256
Reaction score
0
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
 
Joined
Sep 10, 2006
Messages
2,813
Reaction score
1,416
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 Ducking 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
Top