Reply
Query'n SQL with WHERE
Old 07-22-2005, 07:45 AM Query'n SQL with WHERE
Experienced Talker

Posts: 33
Hi! Me again.

I've been trying to figure this one out, but according to all the sites I've been too.. it seems this should work.

This is what I had, and it worked fine:

Code:
$link = @mysql_connect("localhost", "--------", "*******") or die("Could not connect"); 
mysql_select_db ("phases-online_com_-_phasesimagery");
$query="SELECT * FROM contact ORDER BY CID DESC";
$result=mysql_query($query);
$num=mysql_num_rows($result);
I went to mysql, added a field "read", with tinyinit and 1 as the default. So they all have 1 in there. Then I put a simple WHERE in my code:

Code:
$link = @mysql_connect("localhost", "--------", "*******") or die("Could not connect"); 
mysql_select_db ("phases-online_com_-_phasesimagery");
$query="SELECT * FROM contact WHERE read=1 ORDER BY CID DESC";
$result=mysql_query($query);
$num=mysql_num_rows($result);
And now It's telling me " mysql_num_rows(): supplied argument is not a valid MySQL result resource"

I'm doing this so that all of my contact forms will be in my admin area, until I read them and mark them as read with a 2, then they shouldn't show anymore. Am I on the right path?

Sorry to post so frequently, but I really do put alot of effort into trying to figure this stuff out before coming here..

Thanks again guys.

Last edited by Phases : 07-22-2005 at 07:47 AM.
Phases is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 07-22-2005, 08:00 AM
Enigmatic's Avatar
Registered User

Posts: 86
Location: No Fixed Abode
<?php

/*connection stuff*/
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "password";
$dbname = "username_dbName";

$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass")
or die ("Could not connect : " . mysql_error());

$db_select = mysql_select_db("$dbname", $conn)
or die ("Could not select db : " . mysql_error());

$result = mysql_query("SELECT read FROM contact WHERE read = 1 ORDER BY CID DESC");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "Read ID : %s ", $row['read'] "<br />";
}

?>
Enigmatic is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 08:17 AM
Experienced Talker

Posts: 33
Thank you for the speedy reply.

When using this:

Code:
$link = @mysql_connect("localhost", "--------", "*******") or die("Could not connect"); 
mysql_select_db ("phases-online_com_-_phasesimagery");
$result = mysql_query("SELECT read FROM contact WHERE read = 1 ORDER BY CID DESC");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "Read ID : %s ", $row['read'] "<br />";
}
(which is what you gave me minus the connection stuff (more on that in a sec) and I'm getting the following error returned:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/virtual/site200/fst/var/www/html/admin.php on line 50

Line 50 is the echo line.

Also, just to verify, I do want everything pulled out of the table, not just the read column, should I change that "read" after SELECT back to * ?

Lastly, I'm curious, is my method of connecting an unpreferred method? Should I convert to something else?

Thanks again Enig, you've been a big help!

Last edited by Phases : 07-22-2005 at 08:20 AM.
Phases is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 08:24 AM
Enigmatic's Avatar
Registered User

Posts: 86
Location: No Fixed Abode
##----[ FIND ]----##
echo "Read ID : %s ", $row['read'] "<br />";

##----[ REPLACE WITH ]----##
echo "Read ID : %s ," $row['read'] "<br />";

I tend to use the method I placed in the original code because I normally have a file called connections.php or dbs.php then use the include_once('file'); to load it into pages that require connections, means Im not writing the code over and over.

If you find your way easy enough, just stick with it. Each to their own.

* will pull everything from the db.
Enigmatic is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 08:31 AM
Experienced Talker

Posts: 33
Same error

Ah yes. I remember now. The first time I played with sql/php I did it that way. db_connect.php. Twas the way the tutorial taught me to do. This time I decided to be a rebel and do it my own way!
Phases is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 08:54 AM
Enigmatic's Avatar
Registered User

Posts: 86
Location: No Fixed Abode
bah, remove the , then. its not important, just there for aesthitics.
Enigmatic is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 09:00 AM
Experienced Talker

Posts: 33
Same thing..
I juander if it'd help if i posted more of the surrounding code.

Code:
<?php 
$link = @mysql_connect("localhost", "--------", "*******") or die("Could not connect"); 
mysql_select_db ("phases-online_com_-_phasesimagery");
$result = mysql_query("SELECT * FROM contact WHERE read = 1 ORDER BY CID DESC");

while ($num = mysql_fetch_array($result, MYSQL_NUM)) {
echo "Read ID : %s " $num['read'] "<br />";
}

mysql_close();

if ($num==0) {
echo "No Entries.";
} else {

if ($num>5) {
$to=5;
}else{
$to=$num;
}
}

$i=0;
while ($i < $to) {

$name=mysql_result($result,$i,"Name");
$email=mysql_result($result,$i,"Email");
$phone=mysql_result($result,$i,"Phone");
$cid=mysql_result($result,$i,"CID");
$body=mysql_result ($result,$i,"Body");
$date=mysql_result ($result,$i,"Date");

?>


<table width="95%" cellspacing="2" cellpadding="2" border="0">
	<tr>
		<td width="6%"><b>Name:</b><td width="15%"> <? echo $name; ?> <td rowspan="4" width="79%"><b>Message:</b>&nbsp;
		&nbsp;&nbsp; <? echo $body; ?>
	</tr>
	<tr>
		<td width="6%"><b>Email:</b><td width="15%">  <? echo $email; ?>
	</tr>
	<tr>
		<td width="6%"><b>Phone:</b><td width="15%">  <? echo $phone; ?>
	</tr>
	<tr>
		<td width="6%"><b>Date:</b><td width="15%">  <? echo $date; ?>
	</tr>
	<tr>
		<td colspan="3"><hr width="85%" align="center">
	</tr>
	
	 <?php
$i++;
}

echo "</table>";

?>
Thanks again. I'm workin' on it too, not just respondin' and waitin'!

Last edited by Phases : 07-22-2005 at 09:42 AM.
Phases is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 09:31 AM
Experienced Talker

Posts: 33
It's been a long night, and now I must go to the testing center and take a midterm on internet law and business. :| Yay. I'll be back in 'bout an hour and a half or so. Maybe more, maybe less. Just wanted to say thanks for all your help tonight in case you have to jet whiles I'm gone. It's much appreciated!

-Phases
Phases is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 09:34 AM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
Hmmm that looks fine to me perhaps there is another issue on the horizon.
Anyway why not test this script by only looping once. Does this work?

please include the line 50 i.e which echo?
asm is offline
Reply With Quote
View Public Profile Visit asm's homepage!
 
Old 07-22-2005, 09:40 AM
Experienced Talker

Posts: 33
Heya asm, thanks for joinin

Line 50:
Code:
echo "Read ID : %s " $row['read'] "<br />";
Ok out the door, be back asap!
Phases is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 09:43 AM
Enigmatic's Avatar
Registered User

Posts: 86
Location: No Fixed Abode
Just checked the trusty PHP manual ...

echo "Read ID : %s ", $row['read'] "<br />";

should work, although they have it as :

printf("Read ID : %s " $row['read']);
Enigmatic is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 10:59 AM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
ok i could not leave this. i have now messed with this for an hour lol, god loves a try-er , any way I could not get your code to work so I re wrote it to my preferred method but it still did not work: then i got really mad lol, after some messing I altered the database column from read to read1 and that worked, the reason is...well haven’t got a clue but here is the code I used. By the way I think you was using an asso array but specifying a NUM array,

$aNum = 1;
mysql_select_db ("test");
$query = "SELECT * FROM contact WHERE read1 = $aNum "; //OR DIE (mysql_error()) ;
$result = mysql_query($query) OR die("error with accessing database".mysql_error()." ". $query);
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
echo $row[0];
//put more here as needed
// echo $row[1];
}

mysql_close();

also your if has two else – s in it try breaking it down a bit
with

if ($num==0)
{
echo "No Entries.";
}
there is no reason to put an else on this. a better way to write it is
if (!$num)
{
echo "No Entries.";
}
asm is offline
Reply With Quote
View Public Profile Visit asm's homepage!
 
Old 07-22-2005, 11:35 AM
Experienced Talker

Posts: 33
Well guys, Guess what? There is a lesson to be learned here.

After coming home from my test bout 10 minutes ago, I tried both your last suggestions and couldn't get it to work. I don't know why.

So I was like "Hey, I'm gettin' confizzled at this point over what was original and what's been changed and this and that etc"

So, I copied by backup file over. All the original stuff from my first post on here. Then all I did was change "read" to "read1" in the query statement (and the db) because asm mentioned something of the sort.

Everything works fine! So... everything was fine to begin with I guess, just for some reason it won't except "read" as a db name in querys, as far as I can tell.

Will this mystery every be solved? We may never know.

Thanks so much guys for your help and patience with me. Now time for the next task! Viewing said contact forms, marking them as read, and making them go away. Better yet, marking them as read, or "save" or something. Put em in a seperate, easy to get to, area. Oh deary me!

-Phases

(I will go through and clean this up with the suggestions made to me too in a bit . I would like for my code to be as clean as possible. Knowing me, I'll redo the whole site juance it's up and functional and there's no rush.)

Last edited by Phases : 07-22-2005 at 11:40 AM.
Phases is offline
Reply With Quote
View Public Profile
 
Old 07-22-2005, 11:48 AM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
phew i am glad thats sorted!!
asm is offline
Reply With Quote
View Public Profile Visit asm's homepage!
 
Old 07-25-2005, 07:20 PM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
I just read that read is a reserved word in MySQL
asm is offline
Reply With Quote
View Public Profile Visit asm's homepage!
 
Old 07-25-2005, 08:51 PM
Experienced Talker

Posts: 33
There it is!
Phases is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Query'n SQL with WHERE
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.17915 seconds with 12 queries