latino.ad7 Posted February 11, 2010 Share Posted February 11, 2010 I have a problem when using mysql_connect and I can't properly connect to the database. I use WebServ. I had it working OK, but now after reinstall, something is wrong and I can't determine what. I used PHPMyAdmin to set up a table called "objects" in database "mydb". I input one sample record. When I run php code I get the following message: Resource id #3. What's wrong?? The variable $r (see the code below) doesn't seem to include anything. Here is PHP code: $hostt = 'localhost'; $userr = 'root'; $passw = ''; $baz = 'mydb'; $link = mysql_connect ($hostt, $userr, $passw); $db = mysql_select_db($baz, $link); $q = ("Select * from objects"); $r = mysql_query ($q, $link); echo $r; Quote Link to comment Share on other sites More sharing options...
schilly Posted February 11, 2010 Share Posted February 11, 2010 that looks fine. mysql_query returns a resource. now use mysql_fetch_array or mysql_fetch_assoc or mysql_fetch_object on it to get your rows. Quote Link to comment Share on other sites More sharing options...
latino.ad7 Posted February 11, 2010 Author Share Posted February 11, 2010 Ah, certainly. Thank you so much. It works! How do I process this query correctly? It works ok when I add: $row = mysql_fetch_array($r); echo $row[0]; echo $row[1]; But it fails to work in a loop: while ($row = mysql_fetch_array($r)) { echo $row['name']; } How do I type that correctly? Let's say that my table "objects" consists of 3 columns: id, name, author. And I have 2 rows: 0, "aaa", "aaa0" 1, "bbb", "bbb0" Quote Link to comment Share on other sites More sharing options...
schilly Posted February 11, 2010 Share Posted February 11, 2010 this should work as long as you have a db column named "name": while ($row = mysql_fetch_array($r)) { print_r($row); //show whats in the array echo $row['name']; } Quote Link to comment Share on other sites More sharing options...
latino.ad7 Posted February 11, 2010 Author Share Posted February 11, 2010 Thanks. That's what I did finally: $hostt = 'localhost'; $userr = 'root'; $passw = ''; $baz = 'mydb'; $link = mysql_connect ($hostt, $userr, $passw); $db = mysql_select_db($baz, $link); $q = ("Select id, filename, name, author, bin from objects where category = 'mycategory'); $r = mysql_query ($q, $link); while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) { $arrid[$row['id']] = $row['id']; $arrfilename[$row['id']] = $row['filename']; $arrname[$row['id']] = $row['name']; $arrauthor[$row['id']] = $row['author']; $arrbin[$row['id']] = $row['bin']; echo $arrname[0]; 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.