Jump to content

MySQL error, PLEASE HELP!!!


techiefreak05

Recommended Posts

Hi, this code:
[code]<?php
$db_host = "localhost";
$db_user = "zyco";
$db_pwd = "****";
$db_name = "zyco_zycologin";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<center>
<?php
$query = mysql_query("SELECT * FROM messages WHERE to = '".$_SESSION['username']."' AND read = 'no'");
while ($array = mysql_fetch_array($query)) {
echo "Message: ".$array['message'];
}
?>[/code]

produces this result:

[tt]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/zyco/public_html/newlayout/readMessages.php on line 49[/tt]

wahts wrong????
Link to comment
https://forums.phpfreaks.com/topic/17714-mysql-error-please-help/
Share on other sites

!!! uh oh, I did that, and got this:

[tt]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 = 'llamaherder728' AND read = 'no'' at line 1[/tt]

*llamaherder728 is the name of the current username that is trying to retrieve the MySQL Data
So llamaherder728 is your mysql username? If so there must be a clash in variables somewhere in your script. However, you said you were using the variable $db_user.

Try taking out the variables for the connection and putting them straight into the mysql_connect function:
mysql_connect('localhost', 'username','password');
mysql_select_db('dbname');
[code]<?php
$db_host = "localhost";
$db_user = "zyco";
$db_pwd = "****";
$db_name = "zyco_zycologin";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<center>
<?php
$query = mysql_query("SELECT * FROM messages WHERE to = '.$_SESSION[username].' AND read = 'no'");
while ($array = mysql_fetch_array($query)){
echo "Message: ".$array['message'];
echo "From: ".$array['from'];
}
?>[/code]

produces this result:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/zyco/public_html/newlayout/readMessages.php on line 49
try this and show the outcome cheers.

[code]
<?php session_start();

$db_host = "localhost";
$db_user = "zyco";
$db_pwd = "****";
$db_name = "zyco_zycologin";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<center>
<?php
$query = mysql_query("SELECT * FROM messages WHERE to = '.$_SESSION[username].' AND read = 'no'");
echo $query;
while ($array = mysql_fetch_assoc($query)){
echo "Message: ".$array['message'];
echo "From: ".$array['from'];
}
?>
[/code]
the only thing that I can think of is try this for the database connection. Also I would create a database connection file like db.php with this in it. So all that you have to do is include it at the top of your script.

[code]
<?php  
$dbhost = 'yourhost';
$dbusername = 'username';
$dbpasswd = 'password';
$database_name = 'yourdb';

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")  
   or die ("Couldn't connect to server.");
$db = mysql_select_db("$database_name", $connection)
   or die("Couldn't select database.");
?>[/code]

Give this a try.

Good Luck,
Tom
Leave the or die statement on the end of the query, it means you can always see whats going on.

Two things, firstly, if you leave out the single quotes around the session variable then you dont need to use the .s e.g:

$query = mysql_query("SELECT * FROM messages WHERE to = '$_SESSION[username]' AND read = 'no'") or die(mysql_error();

And second, i realise that you need to use the session to get this working properly, i was merely suggesting that you try a sample query in phpMyAdmin to establish wether or not it is a problem with the table, the query or your php code.
[code]<?php
$query = mysql_query("SELECT * FROM messages WHERE to = '.$_SESSION[username].' AND read = 'no'");
echo $query;
while ($array = mysql_fetch_assoc($query)){
echo "Message: ".$array['message'];
echo "From: ".$array['from'];
}
?>[/code]

i needed to turn THAT into THIS:

[code]<?php
$query = mysql_query("SELECT * FROM messages WHERE `to` = '.$_SESSION[username].' AND `status` = 'no'");
echo $query;
while ($array = mysql_fetch_assoc($query)){
echo "Message: ".$array['message'];
echo "From: ".$array['from'];
}
?>[/code]

it seems tow rok.. it doesnt produce any erros.. so hold on, ill check it

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.