Jump to content


Photo

MySQL error, PLEASE HELP!!!


  • Please log in to reply
18 replies to this topic

#1 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 07:36 AM

Hi, this 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'];
 }
?>

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

wahts wrong????
Link shortener with advanced, detailed statistics:

http://tyny.us/

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 07:39 AM

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


#3 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 07:45 AM

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

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

*llamaherder728 is the name of the current username that is trying to retrieve the MySQL Data
Link shortener with advanced, detailed statistics:

http://tyny.us/

#4 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 07:49 AM

Heres my MySQL version:

MySQL Version: 4.1.19-standard
Link shortener with advanced, detailed statistics:

http://tyny.us/

#5 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 07:59 AM

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');

#6 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 08:03 AM

no no no llamaherder728 is the user in my site.. it is NOT the MySQL username, connecting to the DB is fine....
Link shortener with advanced, detailed statistics:

http://tyny.us/

#7 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 08:18 AM

Can anyone see the error?? I have no idea why it wont work!
Link shortener with advanced, detailed statistics:

http://tyny.us/

#8 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 08:24 AM

Me neither. Umm, try doing the same query in phpMyAdmin, but obviously replace ".$_SESSION['username']." with llamaherder728

#9 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 08:29 AM

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..
Link shortener with advanced, detailed statistics:

http://tyny.us/

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 August 2006 - 08:39 AM

post you current code for god sake man?
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#11 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 08:42 AM

<?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'];
 }
?>

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 shortener with advanced, detailed statistics:

http://tyny.us/

#12 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 August 2006 - 08:46 AM

try this and show the outcome cheers.

<?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'];
 }
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#13 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 16 August 2006 - 08:46 AM

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.

<?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."); 
?>

Give this a try.

Good Luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#14 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 08:48 AM

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.


#15 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 08:49 AM

<?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'];
 }
?>

i needed to turn THAT into THIS:

<?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'];
 }
?>

it seems tow rok.. it doesnt produce any erros.. so hold on, ill check it
Link shortener with advanced, detailed statistics:

http://tyny.us/

#16 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 August 2006 - 08:51 AM

mysql_fetch_assoc i new it lol..................
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#17 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 08:52 AM

I got it, it works! thanks everyone!
Link shortener with advanced, detailed statistics:

http://tyny.us/

#18 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 16 August 2006 - 08:57 AM

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.

Link shortener with advanced, detailed statistics:

http://tyny.us/

#19 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 16 August 2006 - 09:30 AM

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


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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users