PDA

View Full Version : [ubuntu] Problem reading mysql table with php



vinaykumarjg
June 15th, 2011, 03:12 PM
Hi all,

I have installed mysql, apache2 and php5. they work fine. But when I try to read a table with php it shows me the blank page. Have i left any thing? I need to know. Here's the code I've used.



<?
$c = mysql_connect("localhost","vinay","vinay");
mysql_select_db("myweb",$c);

$res = mysql_query("select * from member_info",$c);
$data = mysql_fetch_array($res);
print "<pre>";
print_r ($data);
print "</pre>";
?>
Please reply me asap.

Thank you.

Lazaruss
June 15th, 2011, 03:32 PM
I'd guess you're getting a blank page because error reporting is off. is it not mysql_fetch_assoc or mysql_fetch_array?

put the following after the <?php tag

error_reporting(E_ALL);

don't forget to remove it in production

dozycat
June 15th, 2011, 03:51 PM
try this:




$conn = new mysqli('domain', $user, $pwd, 'database') or die ('Cannot open database');



$sql = "SELECT * FROM table";
$result = $conn->query($sql) or die(mysqli_error());
while ($row = $result->fetch_assoc()){
// do something with the current record
}

vinaykumarjg
June 15th, 2011, 04:17 PM
Thanks for the reply. But both of the ways still leaves me blank page. Is there any wrong with the configuration?

dozycat
June 15th, 2011, 04:47 PM
follow this:


http://www.willfitch.com/mysqli-tutorial.html

first case you must call the variables according to your sql string.

Second case the problem could be with your domain string, put an echo after the die. That way you can see if the code is going after die.

Maheriano
June 15th, 2011, 05:00 PM
$sql = mysql_query("select * from member_info");
$sqlRow = mysql_fetch_assoc($sql);
echo "value: " . $sqlRow['column'];


Replace column with the name of the column you want to get the value from.

If that doesn't work, you did something wrong.

vinaykumarjg
June 15th, 2011, 06:01 PM
thanks a lot. It solved my problem. But I have a question. I need to why did the previous code didn't work?
Is there any other ways to solve the problem.


Thanks again.



vinay

vinaykumarjg
June 15th, 2011, 06:08 PM
follow this:


http://www.willfitch.com/mysqli-tutorial.htmlfirst case you must call the variables according to your sql string.

Second case the problem could be with your domain string, put an echo after the die. That way you can see if the code is going after die.



Thats a great tutorial. Thanks dozycat.

dozycat
June 15th, 2011, 06:54 PM
maybe mysql_fetch_assoc instead of mysql_fetch_a?

Maheriano
June 15th, 2011, 07:44 PM
maybe mysql_fetch_assoc instead of mysql_fetch_a?

Yes, you were missing 4 letters from this line.