techiefreak05 Posted August 16, 2006 Share Posted August 16, 2006 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???? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 Add an or die statement:$query = mysql_query("SELECT * FROM messages WHERE to = '".$_SESSION['username']."' AND read = 'no'") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 !!! 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 Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 Heres my MySQL version:[tt]MySQL Version: 4.1.19-standard[/tt] Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 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'); Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 no no no llamaherder728 is the user in my site.. it is NOT the MySQL username, connecting to the DB is fine.... Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 Can anyone see the error?? I have no idea why it wont work! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 Me neither. Umm, try doing the same query in phpMyAdmin, but obviously replace ".$_SESSION['username']." with llamaherder728 Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 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.. Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 16, 2006 Share Posted August 16, 2006 post you current code for god sake man? Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 [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 Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 16, 2006 Share Posted August 16, 2006 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] Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 16, 2006 Share Posted August 16, 2006 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 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 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. Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 [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 Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 16, 2006 Share Posted August 16, 2006 mysql_fetch_assoc i new it lol.................. Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 I got it, it works! thanks everyone! Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 16, 2006 Author Share Posted August 16, 2006 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. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 16, 2006 Share Posted August 16, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.