Page 1 of 1

Unable to obtain output from mysql_query in PHP script

Posted: Tue Sep 08, 2009 12:44 pm
by simonb
Hi All,

I'm new to the forum and new to php so please be gentle with me. I have written a PHP script to output data from a mysql database. Unfortunately the output isn't displayed in my browser. No amount of googling has helped to resolve my problem.

My script is very basic and i'm sure it is just something really simple that I am missing My code is as follows:


<?php

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected Successfully';

$select = mysql_select_db('hardyrods');
if (!$select) {
die('Could not select database: ' . mysql_error());
}

$query = "SELECT * from Demon";
$result=mysql_query($query);


mysql_close($link);

?>


Thanks in advance
Simon

Re: Unable to obtain output from mysql_query in PHP script

Posted: Tue Sep 08, 2009 1:11 pm
by califdon
If that's your complete script, you haven't asked it to display anything. Getting the "result" only means that you have created an array of data in the memory of your web server. It's up to you what to do with that data. You've only taken the first step. You now have to generate HTML to send something to the browser, and you have to deal with such issues as multiple rows returned from your database, etc. In any case, you need to either echo raw data to the browser (ugly!) or send some HTML, including headers. And you need to use one of the mysql_fetch_xxxx() functions to fetch rows of data from your results set. Spend a little more time on a basic php/mysql tutorial (there are a lot of good ones, beginning with w3schools.com and tizag.com) and you'll be up to speed in no time. Come back here with questions.

Re: Unable to obtain output from mysql_query in PHP script

Posted: Tue Sep 08, 2009 2:12 pm
by simonb
califdon wrote:If that's your complete script, you haven't asked it to display anything. Getting the "result" only means that you have created an array of data in the memory of your web server. It's up to you what to do with that data. You've only taken the first step. You now have to generate HTML to send something to the browser, and you have to deal with such issues as multiple rows returned from your database, etc. In any case, you need to either echo raw data to the browser (ugly!) or send some HTML, including headers. And you need to use one of the mysql_fetch_xxxx() functions to fetch rows of data from your results set. Spend a little more time on a basic php/mysql tutorial (there are a lot of good ones, beginning with w3schools.com and tizag.com) and you'll be up to speed in no time. Come back here with questions.
Thanks for the reply Califdon. I told you I was a complete beginner. :lol: I did try using echo to show the output to the browser but I couldn't get that to work. I'll take a look at one of the tutorials.

Re: Unable to obtain output from mysql_query in PHP script

Posted: Tue Sep 08, 2009 2:52 pm
by Mirge
simonb wrote:
califdon wrote:If that's your complete script, you haven't asked it to display anything. Getting the "result" only means that you have created an array of data in the memory of your web server. It's up to you what to do with that data. You've only taken the first step. You now have to generate HTML to send something to the browser, and you have to deal with such issues as multiple rows returned from your database, etc. In any case, you need to either echo raw data to the browser (ugly!) or send some HTML, including headers. And you need to use one of the mysql_fetch_xxxx() functions to fetch rows of data from your results set. Spend a little more time on a basic php/mysql tutorial (there are a lot of good ones, beginning with w3schools.com and tizag.com) and you'll be up to speed in no time. Come back here with questions.
Thanks for the reply Califdon. I told you I was a complete beginner. :lol: I did try using echo to show the output to the browser but I couldn't get that to work. I'll take a look at one of the tutorials.
See:
http://us.php.net/mysql_fetch_array/
http://us.php.net/mysql_fetch_obj/ (My personal favorite)
http://us.php.net/mysql_fetch_row/