mysql_query help?

Hiya 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>
[php]
<?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);
?>
[/php]
    </body>
</html>

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

replace your php code with this one:

[php]
$link = mysql_connect(‘localhost’, ‘root’, ‘’) or die('Could not connect: ’ . mysql_error());

if (isset($_REQUEST[‘name’]))
{
$name=$_REQUEST[‘name’];

$fetch=mysql_query("SELECT * FROM users.userdata WHERE username='$name'") or die die('Invalid query: ' . mysql_error());

if (mysql_num_rows($fetch)==0) 
{
	echo 'Didnt find the user';
}else
{
	echo 'Found him';
}

}

mysql_close($link);
[/php]

let me know if it worked

Use quotation marks not apostrophes if you want to use contractions. Like this…

[php]echo “Didn’t find the user.”;[/php]

However I am always partial to “did not” instead of “didn’t”

Also, you can use the escape character…

echo ‘Didn’t find the user.’;

There are many many escape chars you may use… Such as “\r\n” which is Windows CRLF or carraige return and line feed… Here is a link that lists most of them:
http://php.net/manual/en/regexp.reference.escape.php

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service