Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code check - database query

Status
Not open for further replies.

martinb7

Programmer
Joined
Jan 5, 2003
Messages
235
Location
GB
Hi, can someone check this code for me, whenever I try to use it it comes up with this errors:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\inetpub\ on line 18

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\inetpub\ on line 41

::Code::

Code:
<?php 
// start the session 
session_start(); 
header(&quot;Cache-control: private&quot;); //IE 6 Fix 
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>
<?php include (&quot;config1.php&quot;);

$conn = mysql_connect($host, $user, $pass); // DO NOT CHANGE THIS
mysql_select_db($db, $conn); // DO NOT CHANGE THIS
$query = mysql_query(&quot;SELECT * FROM messages WHERE to = '&quot;.$_SESSION['name'].&quot;'&quot;);

while ($r = mysql_fetch_array($query)){
	$new = $r[&quot;new&quot;];
	
	if($new > 0)
	{
		echo(&quot;<bgsound src=youhavemail.mp3>&quot;);
	}
	if($new == 0)
	{
		echo(&quot;<bgsound src=>&quot;);
	}
}
?>
<body>
<?php 
$user = $_SESSION['name'];

include (&quot;config1.php&quot;);

$conn = mysql_connect($host, $user, $pass); // DO NOT CHANGE THIS
mysql_select_db($db, $conn); // DO NOT CHANGE THIS
$query2 = mysql_query(&quot;SELECT * FROM messages WHERE to = 'MJB3000'&quot;);

while ($r = mysql_fetch_array($query2)){
	$title = $r[&quot;title&quot;];
	$new = $r[&quot;new&quot;];
	$nm += $new;
	
	echo(&quot;<a href=inbox.php>You have $nm message(s) in you Inbox</a>&quot;);
	echo(&quot;$title&quot;); 
}
	?>
<a href=&quot;login.php&quot;>Login </a>
</body>
</html>
any ideas??

Thanx

Martin
 
Try this, I used it on my machine.. I quickly created a table to match what i thought would correspond and I got the script to work by re-writing little changes.. hope it works for you.. test it without thehtml first then add the html.


<?php
// start the session
session_start();
header(&quot;Cache-control: private&quot;); //IE 6 Fix
?>



<?php include (&quot;config1.php&quot;);

$conn = mysql_connect(&quot;$host&quot;, &quot;$user&quot;, &quot;$pass&quot;); // DO NOT CHANGE THIS
mysql_select_db(&quot;$db&quot;, $conn); // DO NOT CHANGE THIS
$query = mysql_query(&quot;SELECT * FROM messages WHERE to LIKE '&quot;.$_SESSION['name'].&quot;'&quot;);

while ($r = mysql_fetch_array($query)){
$new = $r['new'];

if($new > 0)
{
echo &quot;<bgsound src=youhavemail.mp3>&quot;;
}
if($new == 0)
{
echo &quot;<bgsound src=>&quot;;
}
}
?>



<?php
$user = $_SESSION['name'];

include (&quot;config1.php&quot;);

$conn = mysql_connect(&quot;$host&quot;, &quot;$user&quot;, &quot;$pass&quot;); // DO NOT CHANGE THIS
mysql_select_db(&quot;$db&quot;, $conn); // DO NOT CHANGE THIS
$query2 = mysql_query(&quot;SELECT * FROM messages WHERE to LIKE 'MJB3000'&quot;);

while ($r = mysql_fetch_array($query2)){
$title = $r['title'];
$new = $r['new'];
$nm += $new;

echo &quot;<a href=inbox.php>You have $nm message(s) in you Inbox</a>&quot;;
echo &quot;$title&quot;;
}
?>
<a href=&quot;login.php&quot;>Login</a>




Nothing is hard when you realy want to learn it.

Max
 
Sleipnir214 is absolutely right.
In general, do error checking on these MySQL statements:

1. mysql_connect -> the server could be down, the password wrong etc.
2. mysql_select_db -> wrong db name or server down
3. mysql_query -> statement error by malformed SQL, server down

For #3 remember that a SQL query may execute correctly but not return any rows. If you e.g. search for something and nothing is there, it will return true, since the SQL command was successfully executed. You need to check for the presence of rows a different way.
 
I'm no php-wiz, so probably could be done more efficiently, but the number of rows thing could go like this:
Code:
$query2 = mysql_query(&quot;SELECT * FROM messages WHERE to LIKE 'MJB3000'&quot;);
Code:
if mysql_numrows($query2) > 0 {
Code:
    while ($r = mysql_fetch_array($query2)){
        $title = $r['title'];
...snip...

Also, to get good error info, add:
Code:
or die(&quot;Goodbye World: &quot; . mysql_error());

so you get this:
Code:
$conn = mysql_connect(&quot;$host&quot;, &quot;$user&quot;, &quot;$pass&quot;) or die(&quot;Goodbye World: &quot; . mysql_error());

Sorry if this is useless. Cold medicine has me slammed :-)


----
JBR
 
As a quick and dirty diagnostic, can you put the SQL into a variable i.e. $SQL = &quot;select...&quot; and use that in the mysql command rather than the hard coded value. Print the $SQL value before you call mysql command. Also print the the errorcode and errormessage (mysql_error and mysql_errno).

Hopefuly we can get to the botton of the problem.

BTW using like does mysql not want a % somewhere ??

kr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top