|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
mysql_num_rows(): supplied argument is not a valid MySQL result resource in
Could someone here offer me some hint what is wrong with mysql_num_rows():
<html> <head> <title>Book-O-Rama Search Results</title> </head> <body> <h1>Book-O-Rama Search Results</h1> <?php // create short variable names $searchtype=$HTTP_POST_VARS['searchtype']; $searchterm=$HTTP_POST_VARS['searchterm']; $searchterm= trim($searchterm); if (!$searchtype || !$searchterm) { echo 'You have not entered search details. Please go back and try again.'; exit; } $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); @ $db = mysql_connect("localhost","",""); if (!$db) { echo 'Error: Could not connect to database. Please try again later.'; exit; } mysql_select_db('books'); $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = mysql_query($query); $num_results = mysql_num_rows($result); echo '<p>Number of books found: '.$num_results.'</p>'; for ($i=0; $i <$num_results; $i++) { $row = mysql_fetch_array($result); echo '<p><strong>'.($i+1).'. Title: '; echo htmlspecialchars(stripslashes($row['title'])); echo '</strong><br />Author: '; echo stripslashes($row['author']); echo '<br />ISBN: '; echo stripslashes($row['isbn']); echo '<br />Price: '; echo stripslashes($row['price']); echo '</p>'; } ?> </body> </html> |
|
#2
|
||||
|
||||
|
Generally your error means your query didn't execute
I recommend adding an or die() line to your line $result = mysql_query($query); Thus, the line might read: $result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query"); |
|
#3
|
|||
|
|||
|
mysql_num_rows(): supplied argument is not a valid MySQL result resource in
MadCow: After the changes above, I got the following error msg now:
No Database Selected Couldn't execute query: select * from books where author like '%Michael%' Would appreciate your kind assistance. |
|
#4
|
|||
|
|||
|
you did not connect to a database so you cant query anything
you need to do this before you query PHP Code:
|
|
#5
|
|||
|
|||
|
Could not select database!
Bruski: I have changed the line u mentioned above but it's giving me this error msg:
Could not select database! $db_connect=mysql_connect("localhost","","") or die ("Could not connect to database"); mysql_select_db("books") or die ("Could not select database!"); $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query"); $row = mysql_fetch_array($result); $num_results = mysql_num_rows($result); echo '<p>Number of books found: '.$num_results.'</p>'; for ($i=0; $i <$num_results; $i++) { echo '<p><strong>'.($i+1).'. Title: '; echo htmlspecialchars(stripslashes($row['title'])); echo '</strong><br />Author: '; echo stripslashes($row['author']); echo '<br />ISBN: '; echo stripslashes($row['isbn']); echo '<br />Price: '; echo stripslashes($row['price']); echo '</p>'; } |
|
#6
|
||||
|
||||
|
have you created a database on your mysql?
make sure the database "books" exists... I recommend connecting to MYSQL manually (via command line) |
|
#7
|
||||
|
||||
|
Looks like you need to provide a username and password. Additionally, doesn't mysql_query() usually take a resource as the second parameter? Check the docs, and if there's a second parameter, pass $db_connect.
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
|
#8
|
|||
|
|||
|
Database
MadCowDzz: I managed to retrieve the MySQL db (books) in a command line.
dhouston: Mind enlighten me what u mean by second parameter? The problem still persist. Any help would be appreciated. |
|
#9
|
||||
|
||||
|
Read the function documentation at php.net. You can pass a second item to the function call (inside the parentheses). I'm proposing that the function may need your database resource passed explicitly in order to work.
|
|
#10
|
|||
|
|||
|
Access denied for user: ''@'localhost' to database 'books'
I have made some changes to the codes and now getting this error msg:
Access denied for user: ''@'localhost' to database 'books' However, I could access the "books" db in the mysql command prompt using "root" login. Code:
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
// create short variable names
$searchtype=$HTTP_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
exit;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
$my_connection = @mysql_connect('localhost', '', '');
// the "@" is used to supress the generic function error.
if (!$my_connection) { // if value in variable is false (!)
die('Could not connect to MySQL database server, the server returned the error: '.mysql_error());
}
$db = @mysql_select_db('books');
if (!$db) { // if value in variable is false (!)
die('Could not select MySQL database, the server returned the error: '.mysql_error());
}
// Now we can do a query
$query = mysql_query('select * from books where ".$searchtype." like '%".$searchterm."%'');
$result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");
$row = mysql_fetch_array($result);
$num_results = mysql_num_rows($result);
echo '<p>Number of books found: '.$num_results.'</p>';
for ($i=0; $i <$num_results; $i++)
{
echo '<p><strong>'.($i+1).'. Title: ';
echo htmlspecialchars(stripslashes($row['title']));
echo '</strong><br />Author: ';
echo stripslashes($row['author']);
echo '<br />ISBN: ';
echo stripslashes($row['isbn']);
echo '<br />Price: ';
echo stripslashes($row['price']);
echo '</p>';
}
?>
</body>
</html>
|
|
#11
|
|||
|
|||
|
Client does not support authentication protocol requested by server; consider upgradi
When I changed the connection string to my "root" password, I am getting this error msg:
Client does not support authentication protocol requested by server; consider upgrading MySQL client $my_connection = @mysql_connect('localhost', 'root', 'password1'); // the "@" is used to supress the generic function error. if (!$my_connection) { // if value in variable is false (!) die('Could not connect to MySQL database server, the server returned the error: '.mysql_error()); } Hope someone here could offer me some hints ![]() |
|
#12
|
|||
|
|||
|
Hi guys,
Finally,fully solved the problem here. It has something to do with MySQL 4.1 uses an authentication protocal based on a password hashing algorithm that is incompatible with that used by older clients. Details here: http://www.mysql.com/doc/en/Old_client.html Thanks to all guys who have contributed. I appreciate it !! |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > mysql_num_rows(): supplied argument is not a valid MySQL result resource in |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|