jlgray48 Posted April 22, 2007 Share Posted April 22, 2007 This program is suppose to select all records from table yahoo with id = 44 and display the field "name" only. Any help is greatly appreciated. <?php $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("uah",$conn); $sql = "Select name from yahoo where id=44"; mysql_query($sql,$conn) ; $result = mysql_query($sql,$conn); while ($array = mysql_fetch_array($result)) { print "$array['name']"; }; ?> Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/ Share on other sites More sharing options...
unkwntech Posted April 22, 2007 Share Posted April 22, 2007 1st: What output does you code provide to you? 2nd(and nowhere near as important): When you type out SQL syntax (in your code or not) you should allways capitalise the SQL keywords so that it is easyer to read so this: $sql = "Select name from yahoo where id=44"; would look like this: $sql = "SELECT name FROM yahoo WHERE id=44" Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235519 Share on other sites More sharing options...
jlgray48 Posted April 22, 2007 Author Share Posted April 22, 2007 I'm getting no output at the present. Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235522 Share on other sites More sharing options...
Trium918 Posted April 22, 2007 Share Posted April 22, 2007 Would that be the name of a column in the database? Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235526 Share on other sites More sharing options...
jlgray48 Posted April 22, 2007 Author Share Posted April 22, 2007 Would what be the name of a column? Name and ID are my fields. Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235534 Share on other sites More sharing options...
Trium918 Posted April 22, 2007 Share Posted April 22, 2007 Ok what is going on. Are you getting an error or what? Try this: <?php $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("uah",$conn); $sql = "Select name from yahoo where id=44"; $result = mysql_query($sql,$conn) or die("Error".mysql_error()); while ($array = mysql_fetch_array($result)) { print "$array['name']"; } ?> Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235540 Share on other sites More sharing options...
jlgray48 Posted April 22, 2007 Author Share Posted April 22, 2007 No errors just blank page Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235545 Share on other sites More sharing options...
Trium918 Posted April 22, 2007 Share Posted April 22, 2007 No errors just blank page Try what I just posted! Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235551 Share on other sites More sharing options...
trq Posted April 22, 2007 Share Posted April 22, 2007 At the very least, always check your queries succeed before trying to use any results. <?php $conn = mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("uah",$conn); $sql = "SELECT name FROM yahoo WHERE id = 44"; if ($result = mysql_query($sql,$conn)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { print $row['name']; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235556 Share on other sites More sharing options...
jlgray48 Posted April 22, 2007 Author Share Posted April 22, 2007 That works, can we take out all the debugging and make it simple? Do you have to have the if or is that for the debugging? Thanks for all your help. jg Link to comment https://forums.phpfreaks.com/topic/48177-solved-php-code-to-select-certain-records-and-display-only-certain-fields/#findComment-235560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.