Jump to content


Photo

[SOLVED] Why this thing is so difficult in PHP


  • Please log in to reply
2 replies to this topic

#1 ali_kiyani

ali_kiyani
  • Members
  • PipPipPip
  • Advanced Member
  • 61 posts

Posted 24 April 2006 - 11:55 AM

I am new php user and trying to display records from mysql. This thing is so simple in ASP but here it is like a pain in the ass. I tried running it on LOCALHOST (my own computer) and HOSTING SERVER (where my site is hosted) but no luck. Localhost runs PHP version 5.1.1 and Hosting Server runs 4.4.2.

Here is my code:

$username = "root";
$password = "root";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("<BR><font face=arial size=3 color=red>Unable to connect to database. Please try again later.</font>");

mysql_select_db("MyDB",$dbh);


The above connection works and I am able to connect to the database on both LOCALHOST and HOSTING SERVER.

$sql = "select * from tbl_wagest";
$result = mysql_query($sql);

if ($result)
echo "true";
else
echo "false";

$numrows = @mysql_num_rows($result);
echo $numrows;


Now the above piece of code doesn't print $numrows and the IF condition says FALSE which I think means there are no records. But the records are there in the table infact more than 100 but it doesn't print $numrows.

This is happening both LOCALHOST and HOSTING SERVER. What am I doing wrong? :(

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 April 2006 - 12:10 PM

The "false" return from the mysql_query() functions usually means there is a problem with the query's syntax not that there are no rows.

You should always use the "or die" clause an any mysql function, especially when you're debugging code. Use something like:
<?php
$result = mysql_query($sql) or die('There was a problem with the query: <span style="color:red">' . $sql . '<br>' . mysql_error());
?>

Put the "or die" clause in and see what error is displayed. That should give you a hint as to want is wrong.

Ken

#3 ali_kiyani

ali_kiyani
  • Members
  • PipPipPip
  • Advanced Member
  • 61 posts

Posted 24 April 2006 - 12:59 PM

aha!!!! that solved my problem.
Many thanks man!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users