473,809 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Help From Book

Does anyone know what's wrong with this code? I just started learning php &
mysql from a book and this code is copied directly from it, yet it still
fails to work... I always get the following error:

Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result
resource in /home/tjc/public_html/results.php on line 40
<?php
// create short varibles names
$searchtype = $HTTP_POST_VARS['searchtype'] ;
$searchterm = $HTTP_POST_VARS['searchterm'] ;

$searchterm = trim($searchter m) ;

if (!$searchtype || !$searchterm)
{
echo "No search details entered" ;
exit ;
}

$searchtype = addslashes($sea rchtype) ;
$searchterm = addslashes($sea rchterm) ;

@ $db = mysql_pconnect( 'localhost', 'tjc_bookorama' , 'bookorama123') ;

if (!$db)
{
echo "Error: Could not connect to db" ;
exit ;
}

mysql_select_db ("tjc_books" );

$query = "SELECT * FROM books WHERE ".$searchty pe." LIKE
'%".$searchterm ."%'" ;
$result = mysql_query($qu ery) ;

$num_results = mysql_num_rows( $result); // line 40

echo "<p>Number of books found: ".$num_results. "</p>" ;

for ($i = 0; $i <num_results; $i++)
{ {
$row = mysql_fetch_arr ay($result) ;
echo '<p><strong>'.( $i + 1).'. Title: ' ;
echo htmlspecialchar s(stripslashes( $row['title'])) ;
echo '</strong><br />Author: ' ;
echo stripslashes($r ow['author']) ;
echo '<br />ISBN: ' ;

echo stripslashes($r ow['isbn']) ;
echo '<br />Price: ' ;
echo stripslashes($r ow['price']) ;
echo '</p>' ;
}

?>

*************** *************** *************** *************** *************** *
*****

QOTSA: "Nicotine, Valium, Vicodin, Marijuana, Ecstacy and Alcohol"
Einstein: "Imaginatio n is More Important Than Knowledge"
Jul 16 '05 #1
1 2428
> Does anyone know what's wrong with this code? I just started learning php
&
mysql from a book and this code is copied directly from it, yet it still
fails to work... I always get the following error:

Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result
resource in /home/tjc/public_html/results.php on line 40
<?php
// create short varibles names
$searchtype = $HTTP_POST_VARS['searchtype'] ;
$searchterm = $HTTP_POST_VARS['searchterm'] ;

$searchterm = trim($searchter m) ;

if (!$searchtype || !$searchterm)
{
echo "No search details entered" ;
exit ;
}

$searchtype = addslashes($sea rchtype) ;
$searchterm = addslashes($sea rchterm) ;

@ $db = mysql_pconnect( 'localhost', 'tjc_bookorama' , 'bookorama123') ;

if (!$db)
{
echo "Error: Could not connect to db" ;
exit ;
}

mysql_select_db ("tjc_books" );

$query = "SELECT * FROM books WHERE ".$searchty pe." LIKE
'%".$searchterm ."%'" ;
$result = mysql_query($qu ery) ;

$num_results = mysql_num_rows( $result); // line 40

echo "<p>Number of books found: ".$num_results. "</p>" ;

for ($i = 0; $i <num_results; $i++)
{ {
$row = mysql_fetch_arr ay($result) ;
echo '<p><strong>'.( $i + 1).'. Title: ' ;
echo htmlspecialchar s(stripslashes( $row['title'])) ;
echo '</strong><br />Author: ' ;
echo stripslashes($r ow['author']) ;
echo '<br />ISBN: ' ;

echo stripslashes($r ow['isbn']) ;
echo '<br />Price: ' ;
echo stripslashes($r ow['price']) ;
echo '</p>' ;
}

?>


First off, as a beginner, you should not be suppressing MySQL errors. The @
symbol in front of :
@ $db = mysql_pconnect( 'localhost', ...'); will suppress error
messages.

Use a function to return any specific MySQL errors:

For instance:

function the_error ($message) {
$mysql_error = mysql_errno() . ": " . mysql_error();
$page = getenv("REQUEST _URI");
$server = getenv("SERVER_ NAME");
echo "<h1>Sorry, an error has occured!</h1>\n<P>$messag e\n\n";
echo "<P><b>MySQ L Error Info</b><br>$mysql_er ror\n";

echo "<P><A HREF=\"mailto:y o*******@yourem ail.com?subject =Error on
http://$server$page&bo dy=Error Message: $message (mysql error info:
$mysql_error)\" >email the webmaster</a>";
exit;
}
//end error function
//Then connect to your db. pconnect is persistant (Do you need a persistant
connection? If not use mysql_connect.)

$db = mysql_connect(' localhost', 'yourusername', 'yourpass') ;

//Then your query
$sql = "SELECT * FROM yourdatabase";
result = mysql_query($sq l,$db) or the_error("Coul dn't execute get sector
types query: <pre>$sql</pre>");
^
^
Function Call

This will help you enormously for debugging . . .
-JD
Jul 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1214
by: John Spiegel | last post by:
Hi all, I'm trying to efficiently pull data out of an xml file into a XmlDocument AND create another "sub" document based on one subtree of the document. For example, say I've got: <Books> <Book> <Title>Some book</Title> </Book>
2
2517
by: Steven T. Hatton | last post by:
I came across this title while looking for something fairly unrelated (UML diagrams for C++ templates). The review quoted below is solidly positive. I'm not going to be able to run out an buy the book right now. I am still interested in opinions of people who have read it. Learning C++ is on a par with tensor analysis for me. It's damn hard! Anything that reduces the obstacles facing the beginner is worth promotting - IMHO. I'm still...
6
4380
by: Thomas Barth | last post by:
Hi, I'm new to windows programming and still reading a book about windows-programming with C++. I copied the following code from the book into my ide (Eclipse/CDT) to comprehend the code, but two errors occur. In function `LRESULT WndProc(HWND__*, unsigned int, unsigned int, long int)': line 48: error: invalid conversion from `void*' to `HBRUSH__*' line 49: error: invalid conversion from `void*' to `HPEN__*'
1
1425
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. I have marked the section from the book that is of intertest by tagging it with BOOK START HERE and ending with BOOK ENDING HERE. All that text between is just a copy from the book. My question come after the text section. The book says
3
2641
by: Andy S | last post by:
Hi, A couple of guys on the SQL Server forum solved this one yesterday but they used a FULL OUTER JOIN, which, little known to me is missing from Accesss and therefore the JET engine. Can anyone suggest an Access friendly version? I'd like to be able to see other fields from both UNIONed tables. For instance : Positions table: Book SecurityNumber Description
4
2149
by: Terry | last post by:
I need some help refining an MS 2000 relational databse. I have created a simple relational database using two tables, 'Student Details', 'Exam Details' and two forms, 'Input/Edit Exam Details', 'Input/Edit Student Details'. 'Student Details' has a field called 'Log Book No' (no duplicates allowed) and this is the Primary Key. 'Exam Details' also has a field called 'Log Book No' (duplicates allowed) and has no Primary Key, (as each...
4
4430
by: http://www.visual-basic-data-mining.net/forum | last post by:
Besides using For Each /Next loop to find every individual nodes, which loop should be use and how to apply if i want only one set of tag...(in blue) Example: <Book> <Title ID ="1"> <Author>Hello</Author> <Reference web = "www.reference.com" topic = "In thing"> </Title>
4
2755
by: andrei.csibi | last post by:
I've have a .NET Assembly, which is COM Visible. I would like to load objects from this assembly in javascript code. E.g. Assembly Book.dll has the class Book and I would like to use <script language="javascript" type="text/javascript"> var book = new Book(); document.write(book.Title); </script> Even more, if I could access book through the document object it would be even more helpful, like: <script language="javascript"...
6
1237
by: ronrsr | last post by:
here is my result. How do I determine the number of tuples in this array, returned from a mysql database? How do I determine the number of characters or entry in each tuple? thanks very much for your assistance, -rsr-
6
6317
by: Mag Gam | last post by:
Hi All, I am new to XML, and trying to extract some data from a file. The file looks like this: <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY>
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.