Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/47958-solved-displaying-things/
Share on other sites

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>";
?>

 

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();
  }

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.