Beginner: Code doesn't work.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alryaz
Forum Newbie
Posts: 5
Joined: Thu Feb 03, 2011 4:42 am

Beginner: Code doesn't work.

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Beginner: Code doesn't work.

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply