Jump to content

Warning: mysql_fetch_array()


gwolff2005

Recommended Posts

Hi I post this thread under php, because I guess that my coding is wrong.

I always get the following message

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/g/w/o/gwolff2005/html/admin/update.php on line 23

Update data in mysql

 

The code is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php

$host="xxxx"; // Host name
$username="xxxx"; // Mysql username
$password="xxxxx; // Mysql password
$db_name="xxxxx"; // Database name
$tbl_name="sp_users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['user_id'];


// Retrieve data from database
$sql="SELECT * FROM '$sp_users' WHERE id='$user_id'";
$result=mysql_query($sql);
$result=mysql_query("select * from '$sp_users' WHERE id='$user_id'"); 
$rows = mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><div align="center" class="style1">Update data in mysql </div></td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>

<td align="center"><span class="style7">Name</span></td>
<td align="center"><span class="style7">Lastname</span></td>
<td align="center"><span class="style7">Email</span></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['user_first_name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['user_surname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['user_login']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['user_id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection
mysql_close();

?>
</body>
</html>

What am I doing wrong. Thanks for your help!

Link to comment
Share on other sites

Hi I did that. That it is how it looks now...

Probably I cannot concentrate anymore.. I have no idea. where the problem is...

<?php

$host="xxxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="ixxxxt"; // Database name
$tbl_name="xxxx"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['user_id'];


// Retrieve data from database
$sql="SELECT * FROM $sp_users WHERE id=$user_id";
$result=mysql_query($sql);
$result=mysql_query("select * from $sp_users WHERE id=$user_id") or die(mysql_error()); 
$rows = mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><div align="center" class="style1">Update data in mysql </div></td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>

<td align="center"><span class="style7">Name</span></td>
<td align="center"><span class="style7">Lastname</span></td>
<td align="center"><span class="style7">Email</span></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['user_first_name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['user_surname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['user_login']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['user_id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection
mysql_close();

?>
</body>
</html>

Link to comment
Share on other sites

You never assign a value to $sp_users. I think you want to use $tbl_name.

 

Replace

<?php
$sql="SELECT * FROM $sp_users WHERE id=$user_id";
$result=mysql_query($sql);
$result=mysql_query("select * from $sp_users WHERE id=$user_id") or die(mysql_error()); 
?>

with

<?php
$sql="SELECT * FROM $tbl_name WHERE id=$user_id";
$result=mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_query());
?>

 

Ken

 

Link to comment
Share on other sites

Hi Ken,

I did that. Then it tells me again

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

so I took the single quotes away that it looks now like that

<?php
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id= $user_id";
$result=mysql_query($sql);
$result=mysql_query("select * from $tbl_name WHERE id= $user_id") or die(mysql_error()); 
$rows = mysql_fetch_array($result);
?>

then I get

Unknown column 'id' in 'where clause'

:'(

I really appreciate your help...

Link to comment
Share on other sites

Hi Kenrbnsn,

I did that.

Got the following message:

Warning: Wrong parameter count for mysql_query() in /home/content/g/w/o/gwolff2005/html/admin/update.php on line 20

Problem with the query: SELECT * FROM sp_users WHERE id=

 

The whole file is now:

<?php

$host="xxxx"; // Host name
$username="xxx; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxxxt"; // Database name
$tbl_name="sp_users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['user_id'];

?>
// Retrieve data from database
<?php
$sql="SELECT * FROM $tbl_name WHERE id=$user_id";
$result=mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_query());
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><div align="center" class="style1">Update data in mysql </div></td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>

<td align="center"><span class="style7">ID</span></td>
<td align="center"><span class="style7">Name</span></td>
<td align="center"><span class="style7">Lastname</span></td>
<td align="center"><span class="style7">Email</span></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['user_first_name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['user_surname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['user_login']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['user_id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection
mysql_close();

?>
</body>
</html>

Link to comment
Share on other sites

What do you mean?

The site is an updater of a mysql databse. the file "before" is

 

<?php
$host=""; // Host name
$username=""; // Mysql username
$password="; // Mysql password
$db_name=""; // Database name
$tbl_name="sp_users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY user_id";
$result=mysql_query($sql);
?>
<style type="text/css">
<!--
.style2 {font-weight: bold}
.style3 {
font-family: Arial, Helvetica, sans-serif;
color: #000033;
}
.style8 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color: #003333; }
-->
</style>

<table width="486" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td width="427">
  
    <div align="left">
      <table width="486" border="1" cellspacing="0" cellpadding="3">
        <tr>
          <td colspan="4"><div align="center" class="style1 style3"><strong>SchoolPorta Users </strong></div></td>
        </tr>
        <tr>
          <td width="26" align="center"><span class="style2">id</span></td>
          <td width="70" align="center"><span class="style2">Name</span></td>
          <td width="114" align="center"><span class="style2">Lastname</span></td>
          <td width="146" align="center"><span class="style2">Email</span></td>
          <td width="88" align="center"><span class="style2">Update</span></td>
        </tr>
        <?php
while($rows=mysql_fetch_array($result)){
?>
        <tr>
          <td><span class="style8"><? echo $rows['user_id']; ?></span></td>
          <td><span class="style8"><? echo $rows['user_first_name']; ?></span></td>
          <td><span class="style8"><? echo $rows['user_surname']; ?></span></td>
          <td><span class="style8"><? echo $rows['user_login']; ?></span></td>
          <td align="center"><a href="update.php?id=<? echo $rows['user_id']; ?>" class="style8">update</a></td>
        </tr>
        <?php
}
?>
      </table>
    </div></td>
</tr>
</table>
<div align="center">
  <?php
mysql_close();
?>
</div>

as soon as you click there update teh file where teh problem occurs runs.

$user_id is teh column in the mysql database.

Link to comment
Share on other sites

In this chunk of code

<?php

// get value of id that sent from address bar
$id=$_GET['user_id'];

?>
// Retrieve data from database
<?php
$sql="SELECT * FROM $tbl_name WHERE id=$user_id";
$result=mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_query());
?>

 

You assign a value to $id, but in the query you use $user_id. What I'm saying is that you probably want to use $id in your query:

<?php

// get value of id that sent from address bar
$id=$_GET['user_id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id=$id";
$result=mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_query());
?>

 

Ken

 

Link to comment
Share on other sites

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.