Unable to obtain output from mysql_query in PHP script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonb
Forum Newbie
Posts: 2
Joined: Tue Sep 08, 2009 12:34 pm

Unable to obtain output from mysql_query in PHP script

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Unable to obtain output from mysql_query in PHP script

Post 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.
simonb
Forum Newbie
Posts: 2
Joined: Tue Sep 08, 2009 12:34 pm

Re: Unable to obtain output from mysql_query in PHP script

Post 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.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Unable to obtain output from mysql_query in PHP script

Post 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/
Post Reply