/    Sign up×
Community /Pin to ProfileBookmark

Connecting to database with PHP

I found a tutorial thanks to this forum. I went through the tutorial and pretty much figured it out and now I am running into a problem trying ti display the output from my table.

I left t he username and password blank of course.

Here is the error I am getting though when I load the page:

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/kbinc/public_html/test/2.php on line 21

Here is my code:

<html>

<head>

<title>Database Training</title>

</head>

<body>

<?
$username=””;
$password=””;
$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++;
}

?>

</body>

</html>

Thanks in advance for the help.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 17.2005 — Usually means your query didn't work. You should probably check that $result is not false before trying to use it. If it is false, you can ouput mysql_error() (and mysql_errno() too if you want) to see what the error was.
Copy linkTweet thisAlerts:
@venomstyleauthorMay 17.2005 — Thanks for the help, but I just figured it out about 10 mins ago. After looking at it for like an hour I realized I named the table wrong. In the php I had the table named contacts but in the database the table was called test, so I was running a query for a table that didn't excist. Gotta love it.

Maybe you can help me with one other thing:

$num=mysql_num_rows($result);

I am using that to show all the rows. What would I use if I only want to display the like newest 5 or top 5. I am still searching the net for an answer, but all I seem to be finding is how to display all the table with the code like I have above.

Again, thanks a ton for the help.

Venom
Copy linkTweet thisAlerts:
@NogDogMay 17.2005 — Easiest way is to limit your query with the "LIMIT" keyword. For instance, if you want the 5 latest records from an imaginary database:
[code=php]
$query = "SELECT col1,col2,date FROM table ORDER BY date DESC LIMIT 5";
[/code]
Copy linkTweet thisAlerts:
@venomstyleauthorMay 17.2005 — You are the man... thanks so much for the help sir.
×

Success!

Help @venomstyle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...