PHP - Error With Search And Mysql_fetch_array
Getting the following error message: Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in
This error occurs when I first navigate to the search page. When I put a search term in the box and click search it works fine and the error is gone. I've been trying to figure this out for days with no luck, any help you can offer would be greatly appreciated. Code: [Select] <?php require('config.php'); ?> <?php $conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('Could not connect to MySQL database. ' . mysql_error()); mysql_select_db(SQL_DB, $conn); $result = NULL; if (isset($_GET['keywords'])) { $sql = "SELECT id, hostname, ip, clust_name, clust_ip, node_name, ". "node_ip, os, patch " . "FROM serv_main " . "WHERE MATCH (hostname,ip) " . "AGAINST ('" . $_GET['keywords'] . "' IN BOOLEAN MODE) " . "ORDER BY MATCH (hostname,ip) " . "AGAINST ('" . $_GET['keywords'] . "' IN BOOLEAN MODE) DESC"; $result = mysql_query($sql) or die('Could not perform search; ' . mysql_error()); } ?> <html> <body> <form method="get" action="search.php"> <p class="head">Type in the Server name below to search for an SLA</p> <p> <input id="searchkeywords" type="text" name="keywords" <?php if (isset($_GET['keywords'])) { echo ' value="' . htmlspecialchars($_GET['keywords']) . '" '; } ?> > <input id="searchbutton" class="submit" type="submit" value="Search"> </p> <?php echo "<h1>Search Results</h1>\n"; if ($result and !mysql_num_rows($result)) { if (isset($_GET['keywords'])) { $k = $_GET['keywords']; } echo "<p>No SLA for <b><font size=\"6\" color=#990000>" . $k . "</b></font> found.</p>\n"; } else { $bg = ''; //Error occurs right here********************* while ($row = mysql_fetch_array($result)) { //($row['id'], TRUE); $bg = ($bg=='F2F2FF'?'99CCCC':'F2F2FF'); $table = "<table border=\"0\" cellpadding=\"5\">"; $table .= "<tr bgcolor=\"#" . $bg . "\">" . "<td><a href=\"servedit.php?s=" . $row['id'] . "\">" . $row['hostname'] . "</a></td><td>" . $row['ip'] . "</td><td align=\"center\">" . $row['clust_name'] . "</td><td>" . $row['clust_ip'] . "</td><td>" . $row['node_name'] . "</td><td>" . $row['node_ip'] . "</td><td>" . $row['os'] . "</td><td>" . $row['patch'] . "</td></tr>"; } $table .= "</table>"; echo $table; } ?> </form> </body> </html> MOD EDIT: [code] . . . [/code] tags added. Similar TutorialsHi guys! I inputted text into the field 'TSuperQuestion' of my table. Now I am trying to echo that text. I get the following error: Quote Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /webusers/ph6theo/public_html/Code-sandbox.php This is my code: Code: [Select] <?PHP $con = mysql_connect("localhost","ph6theo", "xxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ph6theo_testDB") or die(mysql_error()); $result = mysql_query("SELECT * FROM Q&ATable"); $row = mysql_fetch_array($result); echo $row['TSuperQuestion']; ?> Can anyone help? well, i tried to fetch field from my database using the following code: Code: [Select] $result=mysql_query("SELECT * FROM Persons WHERE username='$myusername'" ); while($row = mysql_fetch_row($result)) now that gives me following error: Code: [Select] Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in.. I'm trying to figure out some pagination on a left joined query, and not having any luck. The query seems to be pulling the entire database, so I went into the query's WHERE and added the table name "descriptors." Now I'm getting this error with the below query: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /data/21/2/40/160/2040975/user/2235577/htdocs/leafsearch4.php on line 35 also: Unknown column 'descriptors.leaf_shape' in 'where clause' I thought this was syntax, but maybe I'm missing something. Code: [Select] $query = "SELECT COUNT(*) as num FROM descriptors LEFT JOIN plantae ON (descriptors.plant_id = plantae.plant_name) WHERE `descriptors.leaf_shape` LIKE '%$select1%' AND `descriptors.leaf_venation` LIKE '%$select3%' AND `descriptors.leaf_margin` LIKE '%$select4%'"; $total_pages = mysql_fetch_array(mysql_query($query)); Hey guys, i was recently making a new PHP code and when i tested it out it gave me this error: Code: [Select] mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\tools\edit_infos.php on line 167 I don't know what the problem is. Here is 'Line 167': Code: [Select] //Line 167 $dnn = mysql_fetch_array(mysql_query('select username,password,email,desc,keys,logo,webclient,forums,hiscores,chatbox,staff,newstitle,newsimage,news,op1,op2,op3 from users where username="'.$_SESSION['username'].'"')); $username = htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); $password = htmlentities($dnn['password'], ENT_QUOTES, 'UTF-8'); $email = htmlentities($dnn['email'], ENT_QUOTES, 'UTF-8'); $desc = htmlentities($dnn['desc'], ENT_QUOTES, 'UTF-8'); $keys = htmlentities($dnn['keys'], ENT_QUOTES, 'UTF-8'); $logo = htmlentities($dnn['logo'], ENT_QUOTES, 'UTF-8'); $webclient = htmlentities($dnn['webclient'], ENT_QUOTES, 'UTF-8'); $forums = htmlentities($dnn['forums'], ENT_QUOTES, 'UTF-8'); $hiscores = htmlentities($dnn['hiscores'], ENT_QUOTES, 'UTF-8'); $chatbox = htmlentities($dnn['chatbox'], ENT_QUOTES, 'UTF-8'); $staff = htmlentities($dnn['staff'], ENT_QUOTES, 'UTF-8'); $newstitle = htmlentities($dnn['newstitle'], ENT_QUOTES, 'UTF-8'); $newsimage = htmlentities($dnn['newsimage'], ENT_QUOTES, 'UTF-8'); $news = htmlentities($dnn['news'], ENT_QUOTES, 'UTF-8'); $op = htmlentities($dnn['op1'], ENT_QUOTES, 'UTF-8'); $opp = htmlentities($dnn['op2'], ENT_QUOTES, 'UTF-8'); $oppp = htmlentities($dnn['op3'], ENT_QUOTES, 'UTF-8'); And here is the whole page code if needed. Code: [Select] <?php include('config.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" /> <title>Edit my personnal informations</title> </head> <body> <div class="header"> <a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a> </div> <?php //We check if the user is logged if(isset($_SESSION['username'])) { //We check if the form has been sent if(isset($_POST['username'], $_POST['password'], $_POST['passverif'], $_POST['email'], $_POST['desc'], $_POST['keys'], $_POST['logo'], $_POST['webclient'], $_POST['forums'], $_POST['hiscores'], $_POST['donate'], $_POST['chatbox'], $_POST['staff'], $_POST['newstitle'], $_POST['newsimage'], $_POST['news'], $_POST['op1'], $_POST['op2'], $_POST['op3'], $_POST['desc'])) { //We remove slashes depending on the configuration if(get_magic_quotes_gpc()) { $_POST['username'] = stripslashes($_POST['username']); $_POST['password'] = stripslashes($_POST['password']); $_POST['passverif'] = stripslashes($_POST['passverif']); $_POST['email'] = stripslashes($_POST['email']); $_POST['desc'] = stripslashes($_POST['desc']); $_POST['keys'] = stripslashes($_POST['keys']); $_POST['logo'] = stripslashes($_POST['logo']); $_POST['webclient'] = stripslashes($_POST['webclient']); $_POST['forums'] = stripslashes($_POST['forums']); $_POST['hiscores'] = stripslashes($_POST['hiscores']); $_POST['donate'] = stripslashes($_POST['logo']); $_POST['chatbox'] = stripslashes($_POST['chatbox']); $_POST['staff'] = stripslashes($_POST['staff']); $_POST['newstitle'] = stripslashes($_POST['newstitle']); $_POST['newsimage'] = stripslashes($_POST['newsimage']); $_POST['news'] = stripslashes($_POST['news']); $_POST['op1'] = stripslashes($_POST['op1']); $_POST['op2'] = stripslashes($_POST['op2']); $_POST['op3'] = stripslashes($_POST['op3']); } //We check if the two passwords are identical if($_POST['password']==$_POST['passverif']) { //We check if the password has 6 or more characters if(strlen($_POST['password'])>=6) { //We check if the email form is valid if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',$_POST['email'])) { //We protect the variables $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $email = mysql_real_escape_string($_POST['email']); $desc = mysql_real_escape_string($_POST['desc']); $keys = mysql_real_escape_string($_POST['keys']); $logo = mysql_real_escape_string($_POST['logo']); $webclient = mysql_real_escape_string($_POST['webclient']); $forums = mysql_real_escape_string($_POST['forums']); $hiscores = mysql_real_escape_string($_POST['hiscores']); $donate = mysql_real_escape_string($_POST['donate']); $chatbox = mysql_real_escape_string($_POST['chatbox']); $staff = mysql_real_escape_string($_POST['staff']); $newstitle = mysql_real_escape_string($_POST['newstitle']); $newsimage = mysql_real_escape_string($_POST['newsimage']); $news = mysql_real_escape_string($_POST['news']); $op = mysql_real_escape_string($_POST['op1']); $opp = mysql_real_escape_string($_POST['op2']); $oppp = mysql_real_escape_string($_POST['op3']); //We check if there is no other user using the same username $dn = mysql_fetch_array(mysql_query('select count(*) as nb from users where username="'.$username.'"')); //We check if the username changed and if it is available if($dn['nb']==0 or $_POST['username']==$_SESSION['username']) { //We edit the user informations if(mysql_query('update users set username="'.$username.'", password="'.$password.'", email="'.$email.'", desc="'.$desc.'", keys="'.$keys.'", logo="'.$logo.'", webclient="'.$webclient.'", forums="'.$forums.'", hiscores="'.$hiscores.'", chatbox="'.$chatbox.'", staff="'.$staff.'", newstitle="'.$newstitle.'", newsimage="'.$newsimage.'", news="'.$news.'", op1="'.$op1.'", op2="'.$op2.'", op3="'.$op3.'" where id="'.mysql_real_escape_string($_SESSION['userid']).'"')) { //We dont display the form $form = false; //We delete the old sessions so the user need to log again unset($_SESSION['username'], $_SESSION['userid']); ?> <div class="message">Your informations have successfuly been updated. You need to log again.<br /> <a href="connexion.php">Log in</a></div> <?php } else { //Otherwise, we say that an error occured $form = true; $message = 'An error occurred while updating your informations.'; } } else { //Otherwise, we say the username is not available $form = true; $message = 'The username you want to use is not available, please choose another one.'; } } else { //Otherwise, we say the email is not valid $form = true; $message = 'The email you entered is not valid.'; } } else { //Otherwise, we say the password is too short $form = true; $message = 'Your password must contain at least 6 characters.'; } } else { //Otherwise, we say the passwords are not identical $form = true; $message = 'The passwords you entered are not identical.'; } } else { $form = true; } if($form) { //We display a message if necessary if(isset($message)) { echo '<strong>'.$message.'</strong>'; } //If the form has already been sent, we display the same values if(isset($_POST['username'],$_POST['password'],$_POST['desc'], $_POST['keys'], $_POST['logo'], $_POST['webclient'], $_POST['forums'], $_POST['hiscores'], $_POST['donate'], $_POST['chatbox'], $_POST['staff'], $_POST['newstitle'], $_POST['newsimage'], $_POST['news'], $_POST['op1'], $_POST['op2'], $_POST['op3'])) { $pseudo = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); if($_POST['password']==$_POST['passverif']) { $password = htmlentities($_POST['password'], ENT_QUOTES, 'UTF-8'); } else { $password = ''; } $email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8'); $desc = htmlentities($_POST['desc'], ENT_QUOTES, 'UTF-8'); $keys = htmlentities($dnn['keys'], ENT_QUOTES, 'UTF-8'); $logo = htmlentities($dnn['logo'], ENT_QUOTES, 'UTF-8'); $webclient = htmlentities($dnn['webclient'], ENT_QUOTES, 'UTF-8'); $forums = htmlentities($dnn['forums'], ENT_QUOTES, 'UTF-8'); $hiscores = htmlentities($dnn['hiscores'], ENT_QUOTES, 'UTF-8'); $chatbox = htmlentities($dnn['chatbox'], ENT_QUOTES, 'UTF-8'); $staff = htmlentities($dnn['staff'], ENT_QUOTES, 'UTF-8'); $newstitle = htmlentities($dnn['newstitle'], ENT_QUOTES, 'UTF-8'); $newsimage = htmlentities($dnn['newsimage'], ENT_QUOTES, 'UTF-8'); $news = htmlentities($dnn['news'], ENT_QUOTES, 'UTF-8'); $op = htmlentities($dnn['op1'], ENT_QUOTES, 'UTF-8'); $opp = htmlentities($dnn['op2'], ENT_QUOTES, 'UTF-8'); $oppp = htmlentities($dnn['op3'], ENT_QUOTES, 'UTF-8'); } else { //otherwise, we display the values of the database $dnn = mysql_fetch_array(mysql_query('select username,password,email,desc,keys,logo,webclient,forums,hiscores,chatbox,staff,newstitle,newsimage,news,op1,op2,op3 from users where username="'.$_SESSION['username'].'"')); $username = htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); $password = htmlentities($dnn['password'], ENT_QUOTES, 'UTF-8'); $email = htmlentities($dnn['email'], ENT_QUOTES, 'UTF-8'); $desc = htmlentities($dnn['desc'], ENT_QUOTES, 'UTF-8'); $keys = htmlentities($dnn['keys'], ENT_QUOTES, 'UTF-8'); $logo = htmlentities($dnn['logo'], ENT_QUOTES, 'UTF-8'); $webclient = htmlentities($dnn['webclient'], ENT_QUOTES, 'UTF-8'); $forums = htmlentities($dnn['forums'], ENT_QUOTES, 'UTF-8'); $hiscores = htmlentities($dnn['hiscores'], ENT_QUOTES, 'UTF-8'); $chatbox = htmlentities($dnn['chatbox'], ENT_QUOTES, 'UTF-8'); $staff = htmlentities($dnn['staff'], ENT_QUOTES, 'UTF-8'); $newstitle = htmlentities($dnn['newstitle'], ENT_QUOTES, 'UTF-8'); $newsimage = htmlentities($dnn['newsimage'], ENT_QUOTES, 'UTF-8'); $news = htmlentities($dnn['news'], ENT_QUOTES, 'UTF-8'); $op = htmlentities($dnn['op1'], ENT_QUOTES, 'UTF-8'); $opp = htmlentities($dnn['op2'], ENT_QUOTES, 'UTF-8'); $oppp = htmlentities($dnn['op3'], ENT_QUOTES, 'UTF-8'); } //We display the form ?> <div class="content"> <form action="edit_infos.php" method="post"> You can edit your informations:<br /> <div class="center"> <label for="username">Username</label><input type="text" name="username" id="username" value="<?php echo $username; ?>" /><br /> <label for="password">Password<span class="small">(6 characters min.)</span></label><input type="password" name="password" id="password" value="<?php echo $password; ?>" /><br /> <label for="passverif">Password<span class="small">(verification)</span></label><input type="password" name="passverif" id="passverif" value="<?php echo $password; ?>" /><br /> <label for="email">Email</label><input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br /> <label for="desc">Webclient/Server Description</label><textarea name="desc" id="desc" rows="3" value="<?php echo $desc; ?>"></textarea><br /> <label for="keys">Webclient/Server Tags<span class="small">(Seperate with comma's)</span></label><input type="text" name="keys" id="keys" value="<?php echo $keys; ?>" /><br /> <label for="logo">Website/Server Logo Link</label><input type="text" name="logo" id="logo" value="<?php echo $logo; ?>" /><br /> <label for="webclient">Webclient Link</label><input type="text" name="webclient" id="webclient" value="<?php echo $webclient; ?>" /><br /> <label for="forums">Forums Link</label><input type="text" name="forums" id="forums" value="<?php echo $forums; ?>" /><br /> <label for="hiscores">Hiscores Link</label><input type="text" name="hiscores" id="hiscores" value="<?php echo $hiscores; ?>" /><br /> <label for="chatbox">Chatbox Html</label><textarea name="chatbox" rows="3" id="chatbox" value="<?php echo $chatbox; ?>" ></textarea><br /> <label for="staff">Staff Page Link</label><input type="text" name="staff" id="staff" value="<?php echo $staff; ?>" /><br /> <label for="newstitle">News Article Title</label><input type="text" name="newstitle" id="newstitle" value="<?php echo $newstitle; ?>" /><br /> <label for="newsimage">News Article Image Link<span class="small">(Displayed under title)</span></label><input type="text" name="newsimage" id="newsimage" value="<?php echo $newsimage; ?>" /><br /> <label for="news">News Article</label><textarea name="news" rows="3" id="news" value="<?php echo $news; ?>" ></textarea><br /> <label for="op1">Optional Link #1</label><input type="text" name="op" id="op" value="<?php echo $op; ?>" /><br /> <label for="op2">Optional Link #2</label><input type="text" name="opp" id="opp" value="<?php echo $opp; ?>" /><br /> <label for="op3">Optional Link #3</label><input type="text" name="oppp" id="oppp" value="<?php echo $oppp; ?>" /><br /> <input type="submit" value="Send" /> </div> </form> </div> <?php } } else { ?> <div class="message">To access this page, you must be logged.<br /> <a href="connexion.php">Log in</a></div> <?php } ?> <div class="foot"><a href="<?php echo $url_home; ?>">Go Home</a> - <a href="http://www.webestools.com/">Webestools</a></div> </body> </html>Anyway, i hope someone can help me solve this. Thanks This is a new error for me. I tried looking through some older posts and so far nothing had fixed it and thus I am stumped. Any idea what could be throwing this error? Code: [Select] <table> <?php //open DB connection include 'dbconn.php'; $sql = "select * tbl_test"; $result = mysql_query($sql,$conn); print $sql; while ($row = mysql_fetch_array($result)){ $status = $row['status']; $space = $row['spacename']; if ($status=="reserved") { echo <<<END <tr> <td style="background-color:#F00; color:#FFF;" align="center">Reserved</td> </tr> END; } else { echo <<<END <tr> <td align="center">$space</td> </tr> END; } } ?> </table> I have this search script, that finds a username in a table and displays it. <form name="search" method="post" action="<?=$PHP_SELF?>"> Search: <input type="text" name="username" /> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <? //This is only displayed if they have submitted the form $searching = $_POST['searching']; $find = $_POST['username']; if ($searching =="yes") { echo "<h2>Results</h2><p>"; //If they did not enter a search term we give them an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // Otherwise we connect to our Database // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term, in the field the user specified $data = mysql_query("SELECT username FROM users WHERE upper($find) LIKE'%$find%'"); //And we display the results while($result = mysql_fetch_array( $data )) { echo $result['username']; echo "<br>"; echo "<br>"; } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches= mysql_num_rows ($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } ?> For some reason it does not work and gives this message: [b]Warning[/b]: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [b]C:\Program Files\EasyPHP5.2.10\www\yanille\users.php[/b] on line [b]153[/b] Error: Unknown column 'MI' in 'where clause' The reason it says MI is because I searched mi in the database which is part of the username in mike123 What would be the problem? I think I did make the query correct. Didn't I? Thanks for the help ahead of time. Hi folks, I am a complete n00b at php and mysql. I am teaching myself from books and the WWW, but alas I am stuck... the error I get is: Parse error: syntax error, unexpected T_STRING in X:\xampp\htdocs\search.php on line 7 here is the code: <?php mysql_connect ("localhost", "user", "password") or die (mysql_error()); mysql_select_db ("it_homehelp_test") or die (mysql_error()); $term = $_POST['term']; $sql = $mysql_query(select * from it_homehelp_test where ClientName1 like '%term%'); <<<------this is line 7 while ($row = mysql_fetch_array($sql)){ echo 'Client Name:' .$row['ClientName1']; echo 'Address:' .$row['Address1']; echo 'Phone:' .$row['Tel1']; } ?> Any help you can offer would be great. I can also post the ".html" file that creates the search bar if it is needed. Thanks Error: Warning: mysql_query() expects parameter 1 to be string, resource given in C:\Program Files\xampp\htdocs\NEW\search.php on line 35 Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\Program Files\xampp\htdocs\NEW\search.php on line 36 Results Sorry, your search: "mod" returned zero results Click here to try the search on google You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3 limit 0,10' at line 1 My PHP code <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("localhost","root",""); //(host, username, password) //specify database ** EDIT REQUIRED HERE ** mysql_select_db("school") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = mysql_query("SELECT * FROM main WHERE username LIKE '%".$trimmed."%' ORDER BY username") or die(mysql_error()) ; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die(mysql_error()) ; // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["1st_field"]; echo "$count.) $title" ; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> Lines: Code: [Select] 35 == $numresults=mysql_query($query); 36 == $numrows=mysql_num_rows($numresults); 59 == $result = mysql_query($query) or die(mysql_error()) ; -- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3 limit 0,10' at line 1 Help Please.. Just take a look at the code.. idk whats wrong. <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("localhost","root",""); //(host, username, password) //specify database ** EDIT REQUIRED HERE ** mysql_select_db("school") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select * from the_table where 1st_field like \"%$trimmed%\" order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["1st_field"]; echo "$count.) $title" ; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> its supposed to search my databases i guess.. i need a site searcher if anyone wanna make me post it please.. or post a tut I'm working on the following assignment and keep coming up with error messages.
"Warning: require(mysqli_connect_registrar.php): failed to open stream: No such file or directory in /home/students/r/b.richards/public_html/cis231/labs/sectionresults.php on line 3 I've included the code I have and any help would be appreciated. Thank you. Assignment Directions: Introduction This lab will demonstrate how to create a dynamic form that is created from database contents. And then that form will be used to pull results from the database, based on the user's selection.
STEP 1 - Create the Database Connection File In your labs directory, define a mysql_connect.php file for the registrar database (with the student | student credentials).
STEP 2 - Create the Form Define the form as sectionsearch.php. Create a dynamic form with a drop-down menu listing available courses by title. (Note: the courseID will be the value that is posted to the results page.)
Step 3 - Create the Results page Define the results page as sectionresults.php. Present the results of the query for sections (unless there are none—then show an appropriate message). If there are results, use an HTML table (dynamically-generated) to display available sections with the following information: Faculty last name Room number Class meeting day(s) Start time End time The information should be sorted by the faculty member's last name. Note: You will need to consult the structure of the database tables to determine which fields need to be drawn from which tables and how to join the appropriate tables needed for the query. Sectionsearch.php code:
<!doctype html>
<head> <body>
<form id="form1" name="form1" method="post" action="sectionresults.php">
<p>Courses
<p>
</form> mysql_connect.php code:
<!doctype html>
//Set database connection information
//Database connection
//Set the encoding sectionresults.php code:
<?php
$cat = "";
$q = "SELECT lastame, FROM faculty WHERE registrar = $cat ORDER BY lastname ASC"; I made a search engine to search my database for specific users ( well I used a tutorial ). I'm not getting any errors atleast when loading the page which is a first for me. However, now I am tackled by something new. When entering the keywords and submitting the search I get: Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server. If you think this is a server error, please contact the webmaster. Error 403 127.0.0.1 04/30/07 11:09:21 Apache/2.2.16 (Win32) PHP/5.3.3 Which is odd for me because I am using the same config.php with connection code to connect to the db which I used with all my other pages. And in those pages I have been able to add/delete/edit and display records from the database tables. Does this have something to do with it being a search engine? Here is my code: Code: [Select] <html> <head> </head> <body> <h2>Search</h2> <form name="search" method="post" action="<?=$PHP_SELF?>"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="fname">First Name</option> <Option VALUE="sname">Last Name</option> <Option VALUE="sno">Profile</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> </body> </html> <?php //This is only displayed if they have submitted the form $searching ='searching'; if ($searching =="yes") { echo "<h2>Results</h2><p>"; //If they did not enter data then they receive an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // Otherwise connect to database include 'includes/config.php'; include 'includes/functions.php'; connect(); // filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); // search for term in specified field $data = mysql_query("SELECT * FROM student WHERE upper($field) LIKE'%$find%'"); // display results while($result = mysql_fetch_array( $data )) { echo $result['fname']; echo " "; echo $result['sname']; echo "<br>"; echo $result['sno']; echo "<br>"; echo "<br>"; } //Counts number or results and display message if there we're non $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } ?> Any ideas? Thanks in advance Hi I am finding that I get a random LDAP search fail the initial time i run a script against 2008R2 when I refresh all is ok ? PHP 5.2.5 get the same thing with the latest version of PHP. any ideas ? its as if the DC is sleeping until you give it a nudge, running on 2008R2 running on vmware. Hi, Im trying to do a search engine. However, im facing this errors.. Anyone can help.. Error : Notice: Undefined variable: searching in C:\wamp\www\i-document\s.php on line 17 Thankz in advance. Code: [Select] <h2>Search Form</h2> <form name="search" method="post" action="s.php"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="file_name">File Name</option> <Option VALUE="year">Year</option> <Option VALUE="downer">Owner</option> < </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php if ($searching =="yes"){ echo "<h3>Search Results</h3><p>"; if ($find == ""){ echo "<p>Please Enter a search term"; exit; } include "config.php"; $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); $query = mysql_query("SELECT * FROM document WHERE upper($field) LIKE'%$find%'"); while($result = mysql_fetch_array($query)){ echo $result['file_ref']; echo " "; echo $result['file_name']; echo "<br>"; echo $result['owner']; echo "<br>"; echo "<br>"; } $matches=mysql_num_rows($query); if ($matches == 0){ echo "Sorry, we can not find an entry to match your query<br><br>"; } echo "<b>Searched For:</b> " .$find; } ?> Hey All, I'm trying to add multiple word search to my site and I'm getting the following error: "mysql_num_rows(): supplied argument is not a valid MySQL result resource " I know, my search is posting because I'm getting my search terms back with the "nothing found" message I have set up. Here's what I have: Code: [Select] $search = $_POST["search"]; $arraySearch = explode(" ", $search); $arrayFields = array(0 => "title", 1 => "content"); $countSearch = count($arraySearch); $a = 0; $b = 0; $query = "SELECT * FROM table1 WHERE desc LIKE '$arraySearch'"; $countFields = count($arrayFields); while ($a < $countFields) { while ($b < $countSearch) { $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'"; $b++; if ($b < $countSearch) { $query = $query." AND "; } } $b = 0; $a++; if ($a < $countFields) { $query = $query.") OR ("; } } $query = $query.")"; $query_result = mysql_query($query); if(mysql_num_rows($query_result) < 1) { echo '<p>No matches found for "'.$search.'"</p>'; } else { Print "<table border cellpadding=3>"; while($row = mysql_fetch_assoc($query_result)) { Print "<th>Description:</th> <td>".$row['desc']. "</td> "; } Print "</table> "; } You guys are the best, thanks so much. //get the search variable from the FORM$function_keywords = mysql_real_escape_string($_POST['function_keywords']);//trim whitespace from the stored variable$trimmed = trim($function_keywords);//separate key-phrases into keywords$trimmed_keywords = explode(" ",$trimmed);// Build SQL Query for each keyword enteredforeach ($trimmed_keywords as $trimm){// MySQL "MATCH" is used for full-text searching.//this code is ebv weird , should check out soon!$query = "SELECT * , MATCH (function_description) AGAINST ('".$trimm."') AS score FROM table_name WHERE MATCH (function_description) AGAINST ('+".$trimm."') ORDER BY score DESC "; // Execute the query to get number of rows that contain search kewords $results=mysql_query ($query,$connection); $row=""; $row =$row . mysql_fetch_array($results, MYSQL_BOTH);} $row_num=mysql_num_rows ($row); if($row_num < 1){ redirect("welcome.php?search=fail"); } else if ($row_num > 1){ ?> <html> <head> <title>Search results</title> </head> <body> <?php foreach($row as $row_result){ echo "function name : {$row_result['function_name']} --- function Description : {$row_result['function_description']}<br/><br/>"; }?> </body> </html>() IS there an error on the SQL syntax ? or its about ? : Code: [Select] $results=mysql_query ($query,$connection); $row=""; $row =$row . mysql_fetch_array($results, MYSQL_BOTH); } $row_num=mysql_num_rows ($row); The dreadful apostrophie problem... This search form returns an error whenever searching with an apostrophie (') Here's the code on the form (html) <td align="center" width="135"><form method="post" action="srch_advert.php"><input type=text name='search' size=15 maxlength=255><br><input type=submit></form></td> <td align="center" width="135"><form method="post" action="srch_details.php"><input type=text name='search' size=15 maxlength=255><br><input type=submit></form></td> <td align="center" width="135"><form method="post" action="srch_artist.php"><input type=text name='search' size=15 maxlength=255><br><input type=submit></form></td> <td align="center" width="135"><form method="post" action="srch_track.php"><input type=text name='search' size=15 maxlength=255><br><input type=submit></form></td> and heres the code on srch_advert.php if ($search) // perform search only if a string was entered. { mysql_connect($host, $user, $pass) or die ("Problem connecting to Database"); $srch="%".$search."%"; $query = "select * from tvads WHERE advert LIKE '$srch' ORDER BY advert, year DESC, details ASC LIMIT 0,30"; $result = mysql_db_query("cookuk_pn", $query); if(mysql_num_rows($result)==0) { print "<h2>Your search returned 0 Results</h2>"; } else if ($result) { Here is my code Code: [Select] // Show the events for this day: $getEvent_sql = "SELECT cal_title, cal_description, date_format(cal_start_time, '%l:%i %p') as fmt_date FROM calendar WHERE month(cal_start_time) = '".$m."' AND dayofmonth(cal_start_time) = '".$d."' AND year(cal_start_time)= '".$y."' ORDER BY cal_start_time"; $getEvent_res = myload($getEvent_sql); echo "<pre>"; print_r($getEvent_res); echo "</pre>"; if (count($getEvent_res) > 0){ $event_txt = "<ul>"; while($ev = mysql_fetch_array($getEvent_res)){ echo "Inside"; echo $ev; $event_title = stripslashes($ev["cal_title"]); $event_shortdesc = stripslashes($ev["cal_description"]); $fmt_date = $ev["fmt_date"]; $event_txt .= "<li><strong>".$fmt_date."</strong>: ".$event_title."<br/>".$event_shortdesc."</li>"; echo $event_title; } $event_txt .="</ul>"; mysql_free_result($getEvent_res); } else { $event_txt = ""; } if ($event_txt != ""){ echo "<p><strong>Today's Events:</strong></p> $event_txt <hr/>"; } I can't get inside the while loop. The array is populated, I've checked that. The count of $getEvent_res = 3. echo "Inside", and echo $ev give me nothing. Any ideas what I'm doing wrong? $getEvent_res = myload($getEvent_sql); The myload is a custom function that just works, I didn't write it, but I know it works. Hi. I just have a quick question. If I perform a mysql query like the one below that returns all of the rows in a table. How do I then add all of this data into an array. If I use the following code bellow the variable $array will only contain the first row of the table. $rs = mysql_query($query, $conn); $array = mysql_fetch_array ($rs); The result pages is supposed to have pagination like google help me please
|