|
 |
|
|
07-22-2005, 08:45 AM
|
Query'n SQL with WHERE
|
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 08:47 AM..
|
|
|
|
07-22-2005, 09:00 AM
|
|
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 />";
}
?>
|
|
|
|
07-22-2005, 09:17 AM
|
|
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 09:20 AM..
|
|
|
|
07-22-2005, 09:24 AM
|
|
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.
|
|
|
|
07-22-2005, 09:54 AM
|
|
Posts: 86
Location: No Fixed Abode
|
bah, remove the , then. its not important, just there for aesthitics.
|
|
|
|
07-22-2005, 10:00 AM
|
|
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>
<? 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 10:42 AM..
|
|
|
|
07-22-2005, 10:31 AM
|
|
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
|
|
|
|
07-22-2005, 10:34 AM
|
|
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?
|
|
|
|
07-22-2005, 10:40 AM
|
|
Posts: 33
|
Heya asm, thanks for joinin 
Line 50:
Code:
echo "Read ID : %s " $row['read'] "<br />";
Ok out the door, be back asap!
|
|
|
|
07-22-2005, 10:43 AM
|
|
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']);
|
|
|
|
07-22-2005, 11:59 AM
|
|
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.";
}
|
|
|
|
07-22-2005, 12:35 PM
|
|
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 12:40 PM..
|
|
|
|
07-22-2005, 12:48 PM
|
|
Posts: 217
Location: UK.Lancashire(true)
|
phew i am glad thats sorted!!
|
|
|
|
07-25-2005, 08:20 PM
|
|
Posts: 217
Location: UK.Lancashire(true)
|
I just read that read is a reserved word in MySQL 
|
|
|
|
07-25-2005, 09:51 PM
|
|
Posts: 33
|
There it is!
|
|
|
|
|
« Reply to Query'n SQL with WHERE
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|