Jump to content

Recommended Posts

Ok.

I'm working on a very simple guestbook, for user profiles.

This is the code;

<?php
session_start();

....DATABASE INFO....

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$user=$_GET['user'];

$sql="SELECT * FROM $tbl_name WHERE to='$user'";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
			<p class="clearboth style1">
			<p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">
                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td><? echo $rows['from']; ?></td>
                      <td><? echo $rows['message']; ?></td>
                      <td> </td>
                    </tr>
                  </table>
</div>
			</p>	
			<div class="clearboth"></div>
		<div class="end"></div>
            <?
}
mysql_close();
?>

 

But I get this error;

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/domainey/public_html/habhub/guestbook.php on line 17

 

Any ideas?

Adam.

Link to comment
https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/
Share on other sites

try...

<?php
session_start();

// ....DATABASE INFO....

$con = mysql_connect($host, $username, $password)or die("cannot connect"); 
$con = mysql_select_db($db_name)or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE to='".$_SESSION['user']."'";
$result = mysql_query($sql) or die(mysql_error());
while($rows = mysql_fetch_array($result)) {
?>
			<p class="clearboth style1">
			<p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">
                 <table width="100%" border="0" cellspacing="0" cellpadding="0">
                   <tr>
                     <td><?php echo $rows['from']; ?></td>
                     <td><?php echo $rows['message']; ?></td>
                     <td> </td>
                   </tr>
                 </table>
</div>
			</p>	
			<div class="clearboth"></div>
		<div class="end"></div>
<?php
}
mysql_close($con);
?>

 

see what error it gives you now.

try...

<?php
session_start();

// ....DATABASE INFO....

$con = mysql_connect($host, $username, $password)or die("cannot connect"); 
$con = mysql_select_db($db_name)or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE to='".$_SESSION['user']."'";
$result = mysql_query($sql) or die(mysql_error());
while($rows = mysql_fetch_array($result)) {
?>
			<p class="clearboth style1">
			<p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">
                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td><?php echo $rows['from']; ?></td>
                      <td><?php echo $rows['message']; ?></td>
                      <td> </td>
                    </tr>
                  </table>
</div>
			</p>	
			<div class="clearboth"></div>
		<div class="end"></div>
<?php
}
mysql_close($con);
?>

 

see what error it gives you now.

 

Thanks. I get this now;

 

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 'to='Adam'' at line 1

<?php

session_start();

 

....DATABASE INFO....

 

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name WHERE to = '". mysql_real_escape_string($user) . "'";

$result=mysql_query($sql);

if (mysql_num_rows($result) > 0)

{

while($rows=mysql_fetch_array($result)){

?>

<p class="clearboth style1">

<p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">

                  <table width="100%" border="0" cellspacing="0" cellpadding="0">

                    <tr>

                      <td><? echo $rows['from']; ?></td>

                      <td><? echo $rows['message']; ?></td>

                      <td> </td>

                    </tr>

                  </table>

</div>

</p>

<div class="clearboth"></div>

<div class="end"></div>

            <?

}

}

else

{

echo "<p>no recipient could be found</p>";

}

mysql_close();

?>

 

is $tbl_name set?

 

is to the correct field?

<?php

session_start();

 

....DATABASE INFO....

 

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name WHERE to = '". mysql_real_escape_string($user) . "'";

$result=mysql_query($sql);

if (mysql_num_rows($result) > 0)

{

while($rows=mysql_fetch_array($result)){

?>

<p class="clearboth style1">

<p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">

                  <table width="100%" border="0" cellspacing="0" cellpadding="0">

                    <tr>

                      <td><? echo $rows['from']; ?></td>

                      <td><? echo $rows['message']; ?></td>

                      <td> </td>

                    </tr>

                  </table>

</div>

</p>

<div class="clearboth"></div>

<div class="end"></div>

            <?

}

}

else

{

echo "<p>no recipient could be found</p>";

}

mysql_close();

?>

 

is $tbl_name set?

 

is to the correct field?

 

Yes, and yes ;)

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.