Page 1 of 1

Beginner: Code doesn't work.

Posted: Fri Feb 04, 2011 8:14 am
by alryaz
I have a source code:

Code: Select all

<?
$username="username";
$password="password";
$database="your_database";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

$i++;
}

?>
and a modified one:

Code: Select all

<?php
$username="products";
$password="***PASSWORD***";
$database="products";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Product List</center></b><br><br>";

$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"first");
$img=mysql_result($result,$i,"img");
$price=mysql_result($result,$i,"price");
$avail=mysql_result($result,$i,"avail");

echo "<br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

$i++;
}

?>
I ALWAYS get an error like:
Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\MySQL_Site\output.php on line 18

What is the problem? What am I doing wrong?

P.S.: This code also can't launch, or launches with an error:
Code:

Code: Select all

<?php
$id=$_GET['id'];
$username="products";
$password="SYECYX6KwMWAGDU9";
$database="products";

mysql_connect('localhost',$username,$password);
$query=" SELECT * FROM jewelry WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$price=mysql_result($result,$i,"price");
$avail=mysql_result($result,$i,"avail");
$img=mysql_result($result,$i,"img");

++$i;
}
Error:
Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\MySQL_Site\product_page.php on line 17
And some other errors:
(argues about "Name" var)
Notice: Undefined variable: name in C:\xampp\htdocs\MySQL_Site\product_page.php on line 46
(argues about "Price" var)
Notice: Undefined variable: price in C:\xampp\htdocs\MySQL_Site\product_page.php on line 50
(argues about "Availible" var)

What's happening?

Re: Beginner: Code doesn't work.

Posted: Fri Feb 04, 2011 9:58 am
by social_experiment

Code: Select all

$num=mysql_numrows($result);
The correct syntax is mysql_num_rows()

Code: Select all

$query=" SELECT name, price, avail, img FROM jewelry WHERE id='$id'";
What happens if you modify your code in the example above? (Related to the 'Undefined variable' errors)

Hth