Darkie Posted April 20, 2007 Share Posted April 20, 2007 Yeah.. I'm teaching myself PHP, so I'm a newb right now. o_o I got this code from a website and edited it to my liking, just so I can learn how to do it. But it doesn't work for me, so I must be doing something wrong. <? //connect to mysql //change user and password to your mySQL name and password mysql_connect("localhost","username","password"); //select which database you want to edit mysql_select_db("members"); //select the table $result = mysql_query("select * from users"); //grab all the content while($r=mysql_fetch_array($result)) { //the format is $variable = $r["nameofmysqlcolumn"]; //modify these to match your mysql table columns $username=$r["username"]; $idnumber=$r["idnumber"]; $password=$r["password"]; $email=$r["email"]; $ip_address=$r["ip_address"]; $signup_date=$r["signup_date"]; $displayname=$r["displayname"]; $vcode=$r["vcode"]; $refer=$r["refer"]; $modded=$r["modded"]; $acctkind=$r["acctkind"]; $lli=$r["lli"]; $money=$r["money"]; //display the row echo "$username <br> $idnumber <br> $modded <br> $signup_date | $password <br>"; } ?> When I use that code, I get this error. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/darkgeo/public_html/test.php on line 13 Help please? Quote Link to comment https://forums.phpfreaks.com/topic/47958-solved-displaying-things/ Share on other sites More sharing options...
AndyB Posted April 20, 2007 Share Posted April 20, 2007 Make this change and report what happens: $result = mysql_query("select * from users") or die("Error: ". mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/47958-solved-displaying-things/#findComment-234356 Share on other sites More sharing options...
Trium918 Posted April 20, 2007 Share Posted April 20, 2007 Try this. <?php //connect to mysql //change user and password to your mySQL name and password mysql_connect("localhost","username","password"); //select which database you want to edit mysql_select_db("members"); //select the table $result = mysql_query("select * from users"); print "<table border=1>\n"; // get field names print "<tr>\n"; while ($field = mysql_fetch_field($result)){ print " <th>$field->name<\th>\n"; }// end while print "</tr>\n\n"; //get row data as an associative array while ($row = mysql_fetch_assoc($result)){ print "<tr>\n"; // look at each field foreach ($row as $col=>$val){ print "<td>$val</td>\n"; }// end foreach print "</tr>\n\n"; }// end while print "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/47958-solved-displaying-things/#findComment-234359 Share on other sites More sharing options...
Darkie Posted April 21, 2007 Author Share Posted April 21, 2007 <3333333 Thank you guys so much for helping me, I've been trying to figure this out for a long time.. Turns out I made a typo in the database name. Haha, I'm.. really something. ^^; Quote Link to comment https://forums.phpfreaks.com/topic/47958-solved-displaying-things/#findComment-234514 Share on other sites More sharing options...
trq Posted April 21, 2007 Share Posted April 21, 2007 You should always error check queries before trying to use the result. The general syntax is.... <?php if ($result = mysql_query($sql)) { // where $sql holds your query. if (mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { // display data. } } else { echo "No records found"; } } else { echo Query failed\n$sql\n" . mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47958-solved-displaying-things/#findComment-234529 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.