Jump to content

Archived

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

techiefreak05

MySQL error, PLEASE HELP!!!

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

Share this post


Link to post
Share on other sites
Add an or die statement:
$query = mysql_query("SELECT * FROM messages WHERE to = '".$_SESSION['username']."' AND read = 'no'") or die(mysql_error());

Share this post


Link to post
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

Share this post


Link to post
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');

Share this post


Link to post
Share on other sites
no no no llamaherder728 is the user in my site.. it is NOT the MySQL username, connecting to the DB is fine....

Share this post


Link to post
Share on other sites
Me neither. Umm, try doing the same query in phpMyAdmin, but obviously replace ".$_SESSION['username']." with llamaherder728

Share this post


Link to post
Share on other sites
no, $_SESSION[username] echo'd to llamaherder728. because thats the user that im logged in as in my site. it HAS to be $_SESSION[username] so ALL my users can read their messages..

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
well, i wanted to expand something.. whats wrong with this:??

echo "From: <a href=.$array['from'].html>$array['from']</a >";

i want to link to the person who its from, cuz every users has a HTML page.

Share this post


Link to post
Share on other sites
change
[code=php:0]echo "From: <a href=.$array['from'].html>$array['from']</a >";[/code]


to
[code=php:0]echo "From: <a href=" . $array['from'] . "html>" . $array['from'] . "</a >";[/code]

Share this post


Link to post
Share on other sites

×

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.