Jump to content

Recommended Posts

Hiay folks!

 

So, I'm creating a search for my website, using Mysql_Query to find the user my 'searcher' was looking for, so, I kind of ran in to a problem. My code runs and, will alway's, print 'Resource ID #4', cause that's apparently what my mysql_query would like to 'echo'. Anyway's, what would happen is, when it reads the variable from the top of my url (Using PHP Arguments), it's using mysql_query to search from my database for the name. If it finds the name, it's suppose to echo, 'found him!', if not then echo, 'didnt find him!'. Code is shown below.

 

<!DOCTYPE html>
<html>
    <head>
        <link REL=StyleSheet HREF="MainCSS.css" TYPE="text/css" MEDIA=screen>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
[code=php:0]
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
$name=$_REQUEST['name'];
$fetch=mysql_query("SELECT * FROM users.userdata WHERE username='$name'");
if (!$fetch) {
    die('Invalid query: ' . mysql_error());    
}
if ($fetch==0) {
    echo 'Didnt find the user';
}
mysql_close($link);
?>

    </body>

</html>

[/code]

 

 

Another question I have, is how do I escape the 's? So I can echo "don't", and stuff?

Link to comment
https://forums.phpfreaks.com/topic/265761-mysql_query-help/
Share on other sites

Sorry, forget to mention this, the way the search works, it by URL addressing. When a user searches in my search bar, it takes them to:

 

localhost/user.php?name=$searchdata

 

And then the above code, would go through and do the mysql_query-ing and stuff.

Link to comment
https://forums.phpfreaks.com/topic/265761-mysql_query-help/#findComment-1361897
Share on other sites

Ah, some of it was my my mistake.  I thought you wanted to echo the user's name if found.

 

To simply see if database rows were returned, do:

 

$query = "SELECT * FROM users.userdata WHERE username = $name";
$result = mysql_query($query);

if (mysql_num_rows($result) > 0) {
   // found
} else {
   // not found
}

 

Of course, you still need to ensure that the query itself worked.  I'll leave that for you as homework. :P

 

If you actually want to do something with the data, then you need to fetch it:

 

$query = "SELECT * FROM users.userdata WHERE username = $name";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

echo $row['username'];

Link to comment
https://forums.phpfreaks.com/topic/265761-mysql_query-help/#findComment-1361903
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.