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
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
Link to comment
Share on other sites

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');
Link to comment
Share on other sites

[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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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
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.