katie77 Posted November 18, 2007 Share Posted November 18, 2007 Hi, I'm trying to learn php and the following code is returning a blank page. Have I done something stupid? I'm using WAMP to test and it's connecting to the database fine. I've also tested the sql in myphpadmin and it's returning the correct variable However on this page it's just returning the html 'hello2' and no variable from the database. Can anyone point out where I'm being stupid? <?php $username="root"; $password=""; $database="test"; $dataset="software"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query= "SELECT * FROM $database.$dataset WHERE name = 'Maya';"; $result=mysql_query($query); ?> <html> Hello2 <?php echo $result;?> </html> Quote Link to comment Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 18, 2007 Share Posted November 18, 2007 put all your php code within your html tags and then remove the second set of <?php ?> tags so that only one set of php tags covers all php script. that should do the trick Quote Link to comment Share on other sites More sharing options...
only one Posted November 18, 2007 Share Posted November 18, 2007 You're not selecting any field, $query = "SELECT * FROM `software` WHERE `name` = 'Maya'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo "{$row['field']}"; Quote Link to comment Share on other sites More sharing options...
katie77 Posted November 18, 2007 Author Share Posted November 18, 2007 Ok So I've followed both your advice! Now I get: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in G:\DEV WEB\Dev Server\test\Ratings.php on line 15 <html> <?php $username="root"; $password=""; $database="test"; $dataset="software"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query= "SELECT * FROM $database.$dataset WHERE name = 'Maya';"; $result=mysql_query($query); $row = mysql_fetch_array($result); echo "{$row['field']}"; ?> </html> Quote Link to comment Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 18, 2007 Share Posted November 18, 2007 k if you want to display al the informatin from the table where name = 'maya' you need a while loop that runs through the table until there are no more rows. while ($row = mysql_fetch_assoc($result)){ $id = $row['PicID']; $name = $row['Name']; } pretty much create variable to hold each value that would be inside of the table, then use echo $id; for example to echo that result. you must do this for each value within that row. normally going echo "Id = ".$id."<BR>"; will produce a readable result. by doing that for each variable(thus each value) you will have a listof everything in the table where name = 'maya' $query= "SELECT * FROM $database.$dataset WHERE name = 'Maya'"; put that in for your query. you had an extra ; at the end which will make it fail. hope that all helps Quote Link to comment Share on other sites More sharing options...
rab Posted November 18, 2007 Share Posted November 18, 2007 You should really check to see if the query failed or succeeded... if( ($result = mysql_query($query)) === False ) { // Errors & die } // continue Quote Link to comment Share on other sites More sharing options...
katie77 Posted November 18, 2007 Author Share Posted November 18, 2007 Thanks - I think I have made the problem more complicated by putting SELECT *. I will start again: I have made the following table: name cost_pound Maya3000 Max3200 What I would like is an html page which displays cost_pound WHERE name = 'Maya'. That is, an html page displaying '3000'. That's it. Sorry I wasn't very clear. Thanks again Quote Link to comment Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 18, 2007 Share Posted November 18, 2007 select * should work. what that does is return all values within the row that contains the name Maya <html> <?php $username="root"; $password=""; $database="test"; $dataset="software"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query= "SELECT * FROM $database.$dataset WHERE name = 'Maya'"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)){ $name = $row['name]; $cost = $row['cost_pound']; echo "Name = ".$name."<BR>"; echo "Cost = ".$cost."<BR>"; } // Closing the Database $result = mysql_close($link); if(!$result) { echo "Couldn't close the Database Properly<BR>"; } ?> </html> try this code and see if it works. also i added in closing the database. won't cause too many problems right now but may in the future. Quote Link to comment Share on other sites More sharing options...
katie77 Posted November 18, 2007 Author Share Posted November 18, 2007 hi there, thanks for the help so far! I think you might have missed an apostrophe in $name = $row['name]; so I added that after name, however it now says: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in G:\DEV WEB\Dev Server\test\Ratings.php on line 15 Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in G:\DEV WEB\Dev Server\test\Ratings.php on line 23 Couldnt close the Database Properly What does this mean? Quote Link to comment Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 18, 2007 Share Posted November 18, 2007 haha ah yes i forgot you connected to the database differently than i do....for the sake that i've never done anyother way. try // Opening the Database $link = mysql_connect(localhost,$username,$password); if(!$link) { echo "The link was in error <BR>"; exit(); } // Select a Database $db_link = mysql_select_db($database, $link); if(!$db_link ) { echo "Selecting a Database failed!<BR>"; mysql_close($link); // Close the Connection exit(); } that's for connecting and choosing the database to work with. try changing the query to $query= "SELECT * FROM $dataset WHERE name = 'Maya'"; this "should" work Quote Link to comment Share on other sites More sharing options...
katie77 Posted November 18, 2007 Author Share Posted November 18, 2007 hmm blank page again - just to check here's my code in full: <html> <?php $username="root"; $password=""; $database="test"; $dataset="software"; // Opening the Database $link = mysql_connect(localhost,$username,$password); if(!$link) { echo "The link was in error <BR>"; exit(); } // Select a Database $db_link = mysql_select_db($database, $link); if(!$db_link ) { echo "Selecting a Database failed!<BR>"; mysql_close($link); // Close the Connection exit(); } $query= "SELECT * FROM $dataset WHERE name = 'Maya'"; ?> </html> Quote Link to comment Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 18, 2007 Share Posted November 18, 2007 do you sill have after the $query $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)){ $name = $row['name]; $cost = $row['cost_pound']; echo "Name = ".$name."<BR>"; echo "Cost = ".$cost."<BR>"; } Quote Link to comment Share on other sites More sharing options...
katie77 Posted November 18, 2007 Author Share Posted November 18, 2007 No i didn't :-\- I added it and now I get error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in G:\DEV WEB\Dev Server\test\Ratings.php on line 31 I've pasted my complete code below... <?php $username="root"; $password=""; $database="test"; $dataset="software"; // Opening the Database $link = mysql_connect(localhost,$username,$password); if(!$link) { echo "The link was in error <BR>"; exit(); } // Select a Database $db_link = mysql_select_db($database, $link); if(!$db_link ) { echo "Selecting a Database failed!<BR>"; mysql_close($link); // Close the Connection exit(); } $query= "SELECT * FROM $dataset WHERE name = 'Maya'"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)){ $name = $row['name']; $cost = $row['cost_pound']; echo "Name = ".$name."<BR>"; echo "Cost = ".$cost."<BR>"; } ?> </html> Quote Link to comment Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 18, 2007 Share Posted November 18, 2007 alllll right. so something is going wrong withyour select statement. just for trying's sake try $query= "SELECT * FROM software WHERE name = 'Maya'"; is "software" the name of the table you want to read from? if it is....enter the select statement above Quote Link to comment Share on other sites More sharing options...
katie77 Posted November 18, 2007 Author Share Posted November 18, 2007 oh gawd... almost too embarrassed to reply - your answer prompted me to check and I was getting muddled with another database, so I changed it and it works perfectly... so sorry for being stupid and thanks for helping me get to the bottom of it...! :-X Can I ask what software you use for php? I'm not sure if there's any shortcuts - for example do you write out the connect code each time or do you use software that let's you click a button to add the 'connect' script etc? (as you can tell I need all the help I can get! ) Quote Link to comment Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 18, 2007 Share Posted November 18, 2007 no with each php page i write that uses a database you have to write out the code for connecting and selecting. I use Adobe Dreamweaver for scripting and use a separate ftp software for uploading. if you use an Apple computer... cyberduck is a very god program for uploading and if you use Windows, filezilla is pretty good too. i'd avoid using dreamweavers built in ftp uploader as i've not had much luck with it in the past. i'm glad you got it all working now. have a good day! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.