PDA

View Full Version : I think i made a noob error


barkermn01
03-07-2008, 06:12 PM
i been trying to read about how to do a query using AND but i keep getting a stupid error
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/home1/barkersm/public_html/IM/view.php</b> on line <b>38</b><br />

$query = mysql_query("SELECT * FROM `Messages` WHERE (To='$TO' AND From='$FROM') ORDER BY `ID`");
while($row = mysql_fetch_array($query))

awatson
03-07-2008, 06:20 PM
I believe your while statement should look like this:


while ($row = mysql_fetch_array($result)) {

barkermn01
03-07-2008, 06:22 PM
i dont think so coz $result is empty the query gets returned to the Variable while mysql_fetch_array gets the results once at a time
I know it is nota PHP Error other wise i would not see hte MySQL Error it is some thing to do with the AND i have on it

_Aerospace_Eng_
03-07-2008, 06:24 PM
Your query is likely erroring out. And no it shouldn't be $result as thats not the name of his mysql_query resource. Try this
$sql = "SELECT * FROM `Messages` WHERE To='$TO' AND From='$FROM' ORDER BY `ID`";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query))
Check to make sure that $TO and $FROM are lowercase. Also I sure hope you are taking security measures to prevent mysql injection such as using mysql_real_escape_string.

http://www.ilovejackdaniels.com/php/writing-secure-php/

barkermn01
03-07-2008, 06:26 PM
ok i ddi that so then i put in AdminB by hand and i am still getting an SQL Error

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='AdminB' AND From='AdminA' ORDER BY `ID`' at line 1


$FROM = $_SESSION['User'];
if($FROM == ''){$FROM = $_GET['From'];}
$TO = mysql_escape_string($_GET['User']);
if($TO == ''){$TO = 'AdminB';}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
#apDiv1 {
position:absolute;
width:245px;
height:197px;
z-index:1;
left: 0px;
top: 0px;
}
-->
</style>
</head>

<body>
<div id="apDiv1" align="center">
<textarea name="convo" cols="40" rows="12" class="convo" id="convo"> <?php
// Connect to Database
$con = mysql_connect($server, $user, $pass);
$db = mysql_select_db($db, $con);

// Fethch All Comments from SQL
$sql = "SELECT * FROM `Messages` WHERE To='$TO' AND From='$FROM' ORDER BY `ID`";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query))
This is all the main of my Site

This is the PHPmySQL Output
ID From To Date Message
1 AdminA AdminB 7 This is a Test Message

_Aerospace_Eng_
03-07-2008, 07:10 PM
TO is a mysql reserved word as is FROM. You need to change your column name. Here is a list of reserved words for mysql 5.

http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-0.html

tomws
03-07-2008, 08:00 PM
TO is a mysql reserved word as is FROM. You need to change your column name.

Backticking those names would solve the problem, yes?

Fumigator
03-07-2008, 11:34 PM
It's a much much better idea to give your tables and columns good, descriptive names.

barkermn01
03-08-2008, 02:23 PM
ok and they are lol
to - who the message going to
from - who the message came from

Lol
Have to do some thing like
Sent - Received

I NEW it was a NOOB Error HEHE

EDIT
******************************************************
Yay Thx ppl it is working now
http://www.barkersmedia.co.uk/IM/index.php?From=AdminA&User=AdminB
My msg is being show thx for the help

CFMaBiSmAd
03-08-2008, 03:31 PM
Back-ticks ` are mysql specific. If you ever need to move your application to a different sql server or mysql chooses to more closely follow standards in the future, you will need to rename your columns anyway. It is better to create a design that is not dependent on non-standard syntax.

Fumigator
03-08-2008, 04:28 PM
to - who the message going to
from - who the message came from

Realize that if you have to explain what the name means, it's not a good name. "to" could mean a thousand different things, especially for someone who isn't familiar with what the data in the table represents.

How about--


message_recipient
message_sender


No chance of accidentally using a reserved word, and you know exactly what those columns store without needing any explanation.

All in good time, though, my friend-- as you said, you are a noob and you can't be expected to be writing good code and designing good database schemas yet. Practice, practice! :thumbsup:

barkermn01
03-09-2008, 06:26 AM
Help New Error Bit ****ed up this one


<?php require_once('session.php');
include('config.php');
?>
<?php include('cleaner.php'); ?>
<?php
/* This will send the message to the server<br />
* Using MySQL Querys we will send the data to a table for all chats
*/

// Grab all the Gets for the Message

$MSG = mysql_escape_string($_GET['MSG']);
$Send = $_SESSION['User'];
if($Send == ''){$Send = $_GET['From'];}
$recv = mysql_escape_string($_GET['User']);
$TIME = date("j");

// Connect to Database
$con = mysql_connect($server, $user, $pass);
$db = mysql_select_db($db, $con);

// Coment to Output form
$output = ''.$FROM.'Says: '.$MSG.'
';

// Add Info to DB
$sql = "INSERT INTO `Messages` (`ID`, `Sent`, `Recived`, `Date`, `Message`)
VALUES(NULL, '$Send', '$recv', '$TIME', '$MSG');";
$query = mysql_query($sql) or die(mysql_error());
mysql_query($query, $con);

//Close My SQL Connection
mysql_close($con);


This is using the same settings file and Copyed and pasted Connection as the other one and for some reason i get this wack error with mysql_error();
mysql_error();

No database selected

_Aerospace_Eng_
03-09-2008, 05:00 PM
Look at this line
$db = mysql_select_db($db, $con);
You say to select a database that was assigned the $db variable yet you set the mysql_select_db to the $db variable instead. Make sure your $db variable is an actual database. Also you should be using mysql_real_escape_string not mysql_escape_string (deprecated).

The error isn't "wack". If you pay attention to what you code and actually understand it rather than copy and paste stuff you shouldn't have simple issues like this.