Jump to content

[SOLVED] Why this thing is so difficult in PHP


ali_kiyani

Recommended Posts

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:

[b]$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);[/b]

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

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

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

$numrows = @mysql_num_rows($result);
echo $numrows;[/b]

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? :(
Link to comment
Share on other sites

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:
[code]<?php
$result = mysql_query($sql) or die('There was a problem with the query: <span style="color:red">' . $sql . '<br>' . mysql_error());
?>[/code]

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

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.