mac_shinobi Posted October 19, 2008 Posted October 19, 2008 Hi I have phpmyadmin setup on my web server and have created a database called gecko with one table called feedback. I am trying to create a php page that will execute a the Select field1, field2, field 3 from feedback but am just getting it returning Connected Succesfully and Resource id #3 What does that mean and how do I fix it ? $user="gecko"; $host='localhost:/var/run/mysqld/mysqld.sock'; $password="password"; $database="gecko"; $link = mysql_connect($host, $user, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; ?> $sql = 'SELECT Forename , Surname , Comment FROM feedback; '; $result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql); //do the query print($result); mysql_close($link); ?> I did find this : PHP/MYQSL brings back wrong value (resource ID #3) - Dynamic Drive Forums which basically says "You cannot echo your raw query. You must first grab the information it is returning with a function such as mysql_fetch_array()" Can someone correct my php code so that it displays what I have in my database table feedback.
Jona Posted October 19, 2008 Posted October 19, 2008 (edited) something like print_r(mysql_fetch_array($result)); Should do the job, to display it in a more formatted way you'll want to assign the array to a variable and then do a for loop such as: $rows = mysql_fetch_array($result); foreach($rows as item){ print $item['field1']; print $item['field2']; } Hope that helps. Cheers Jona Ninja edit: To correct typo. Edited October 19, 2008 by Jona
Gatt Posted October 19, 2008 Posted October 19, 2008 if u have PHPMyAdmin running, then tehre should be an option somehwere to run a query, then get it do display the PHP coding for that query..
mac_shinobi Posted October 19, 2008 Author Posted October 19, 2008 something like print_r(mysql_fetch_array($result)); Should do the job, to display it in a more formatted way you'll want to assign the array to a variable and then do a for loop such as: $rows = mysql_fetch_array($result); foreach($rows as item){ print $item['field1']; print $item['field2]; } Hope that helps. Cheers Jona hi ok tried that and got this error : Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /home/gecko/www/feedback.php on line 19
Gatt Posted October 19, 2008 Posted October 19, 2008 $rows = mysql_fetch_array($result); foreach($rows as item){ print $item['field1']; print $item['field2]; } Should read $rows = mysql_fetch_array($result); foreach($rows as item){ print $item['field1']; print $item['field2']; } missing ' after field2
Jona Posted October 19, 2008 Posted October 19, 2008 As above, typing error, will correct in the orignal post.
mac_shinobi Posted October 19, 2008 Author Posted October 19, 2008 ok lets try again : $link = mysql_connect($host, $user, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; ?> $sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30'; $result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql); //do the query $rows = mysql_fetch_array($result); foreach($rows as item){ print $item['Forename']; print $item['Surname']; print $item['Comment']; } mysql_close($link); ?> I get that error using the above code which has the commas etc in the correct place yet if I put a dollar symbol on this part : $rows = mysql_fetch_array($result); foreach($rows as $item){ print $item['Forename']; print $item['Surname']; print $item['Comment']; } As you can see in the foreach part I have put a dollar symbol aka $ before the item and then each time I run it it just returns mmmmmm What good is that - I have emptied my database and have not got any fields then I go to my add.php which works fine and it adds a record into the database but I have not once entered mmmmmm
Gatt Posted October 19, 2008 Posted October 19, 2008 (edited) ignore this... Edited October 19, 2008 by Gatt
mac_shinobi Posted October 19, 2008 Author Posted October 19, 2008 As you can see from my PHP above I only want it to return $sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30'; The Forename, Surname and Comment that each user has made in the order they were posted. What PHP do I need to do this please.
Gatt Posted October 19, 2008 Posted October 19, 2008 This is an exert from one of my PHP scripts: $query = "SELECT * FROM dvds ORDER BY dvd_name ASC"; $result = mysql_query($query); $numofrows = mysql_num_rows($result); while ($i < $numofrows) : $information = mysql_result($result, $i, "dvd_name"); $i++; endwhile; ?> SO yours would be: $query = "SELECT Forename, Surname, Comment FROM Feedback LIMIT 0, 30"; $result = mysql_query($query); $numofrows = mysql_num_rows($result); while ($i < $numofrows) : $information = mysql_result($result, $i, "forename"); $i++; endwhile; ?>
mac_shinobi Posted October 19, 2008 Author Posted October 19, 2008 This is an exert from one of my PHP scripts: $query = "SELECT * FROM dvds ORDER BY dvd_name ASC"; $result = mysql_query($query); $numofrows = mysql_num_rows($result); while ($i < $numofrows) : $information = mysql_result($result, $i, "dvd_name"); $i++; endwhile; ?> SO yours would be: $query = "SELECT Forename, Surname, Comment FROM Feedback LIMIT 0, 30"; $result = mysql_query($query); $numofrows = mysql_num_rows($result); while ($i < $numofrows) : $information = mysql_result($result, $i, "forename"); $i++; endwhile; ?> ok tried that and am getting this : Connected successfully Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gecko/www/feedback.php on line 19 The script I am using is this : $user="gecko"; $host='localhost:/var/run/mysqld/mysqld.sock'; $password="mypassword"; $database="gecko"; $link = mysql_connect($host, $user, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; ?> $sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30'; //$result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql); //do the query //$query = "SELECT Forename, Surname, Comment FROM Feedback LIMIT 0, 30"; $result = mysql_query($sql); $numofrows = mysql_num_rows($result); while ($i < $numofrows) : $information = mysql_result($result, $i, "forename"); $i++; endwhile; mysql_close($link); ?>
mac_shinobi Posted October 19, 2008 Author Posted October 19, 2008 ok have this code now : $user="gecko"; $host='localhost:/var/run/mysqld/mysqld.sock'; $password="mypassword"; $database="gecko"; $link = mysql_connect($host, $user, $password); mysql_select_db("gecko", $link); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; ?> //$sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30'; //$result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql); //do the query $query = "SELECT Forename, Surname, Comment FROM feedback LIMIT 0, 30"; $result = mysql_query($query, $link); $numofrows = mysql_num_rows($result); while ($i < $numofrows) : $information = mysql_result($result, $i, "Forename"); $i++; endwhile; mysql_close($link); ?> It just states that its connected successfully but no errors so how do I make it print each result ie Forname, Surname and Comment ??
Gatt Posted October 19, 2008 Posted October 19, 2008 Sorry my fault - add a line to decalre what $i is $i=0 Just after your DB details: $user="gecko"; $host='localhost:/var/run/mysqld/mysqld.sock'; $password="mypassword"; $database="gecko"; $i=0; //Declare counter here.. $link = mysql_connect($host, $user, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; ?> $sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30'; //$result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql); //do the query //$query = "SELECT Forename, Surname, Comment FROM Feedback LIMIT 0, 30"; $result = mysql_query($sql); $numofrows = mysql_num_rows($result); while ($i < $numofrows) : $information = mysql_result($result, $i, "forename"); $information = mysql_result($result, $i, "Surname"); $information = mysql_result($result, $i, "Comment"); $i++; endwhile; mysql_close($link); ?> Does it work now?
mac_shinobi Posted October 19, 2008 Author Posted October 19, 2008 Nope getting this error : Connected successfully Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gecko/www/feedback.php on line 21
Gatt Posted October 19, 2008 Posted October 19, 2008 Hmm.. I can only imagine its those quote marks.. Here is my full php script.. include ("includes/db_connect.php"); ?> DVD Library - echo gmdate("l, dS F, Y"); ?> DVD Library.... $counter = 0; ?> </pre><form method="post" action="vew_disc.php"> $query2 = "SELECT * FROM dvds ORDER BY dvd_name ASC"; $result2 = mysql_query($query2); $numofrows2 = mysql_num_rows($result2); while ($i < $numofrows2) : $information = mysql_result($result2, $i, "dvd_name"); print "$information"; $i++; endwhile; ?> </form> Also, this is how PHPMyAdmin generated the PHP Code for another query.. $sql = 'SELECT Season , Episode_Title , Stardate FROM `tng` LIMIT 0, 30 ';
PaulBM Posted October 21, 2008 Posted October 21, 2008 (edited) If you paste the SQL query... SELECT Forename , Surname , Comment FROM feedback into phpmyadmin's SQL page and run it do you get data back? If the num_rows call is failing with the error you posted, it looks like the result set has an error or is null. After $result = mysql_query($sql); check that $result is valid, before beginning to display the results. For debug purposes... print mysql_errno()." : ".mysql_error().""; Zero is what you're looking for. Edited October 21, 2008 by PaulBM
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now