Forum Moderators: coopster

Message Too Old, No Replies

SQL/PHP error

Can someone spot the error?

         

ahmedtheking

12:51 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Heres some php code to run a 2nd query on a DB connection! The DB is already connected via a config script. The script works for all the switches apart from the default!

If anyone can spot the error, please tell! I get nothing from the default!

<?php

// get subs

switch ($strip[0]) {

case "cl":
$Query2 = "SELECT * FROM Clients where pgname LIKE 'cl%' AND pgname NOT LIKE 'cl_index'";
break;

case "sv":
$Query2 = "SELECT * FROM $TableName where pgname LIKE 'sv%' AND pgname NOT LIKE 'sv_index'";
break;

case "cn":
$Query2 = "SELECT * FROM $TableName where pgname LIKE 'cn%' AND pgname NOT LIKE 'cn_thanks, cn_index'";
break;

default:
$Query2 = "SELECT * FROM $TableName where pgname LIKE 'pr_part1, pr_whywebsite, privacy, accessibility'";

break;

}

// do DB script:
// start 2nd query
$counter = "1";
$Result2 = mysql_db_query ($DBName, $Query2, $Link);
while ($Row2 = mysql_fetch_array ($Result2)) {

// do strip for the cl images and stuff

$strip2 = explode("_",$Row2[pgname]);

// done DB now do a loop...
print ("<li><a href=\"index.php?goto=$Row2[pgname]\" style=\"text-transform:capitalize;\" title=\"$Row2[title]\" accesskey=\"" . $counter . "\">$Row2[title]</a></li>");
$counter++; // for the tabindex
// close query...
}
mysql_free_result($Result2);
?>

coopster

1:22 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What error are you receiving? The query statement syntax looks acceptable. You can check the syntax by echoing the variables value to the browser to be sure. If the syntax is indeed what you expect you could also echo the number of rows returned using mysql_num_rows() [php.net] to see if you are getting any rows returned.

ahmedtheking

1:33 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well this is the thing, I'm not recieving any errors! Nothing displays! Ive checked the DB to make sure i have selected the right rows!

Donno what's going on!

coopster

2:21 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's why I told you to run the mysql_num_rows() function, to see if anything is going on (any data being found and returned) ;)

Try it, right after your switch statement. By the way, the mysql_db_query() [php.net] function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() [php.net] and mysql_query() [php.net] instead:

} // this is the end of your switch statement. 
// This next statement doesn't have to be here,
// it could be with your connection stuff
// Just wanted to show you how it is used.
mysql_select_db($DBName, $Link);
// now see if we got anything returned:
$Result2 = mysql_query($Query2, $Link);
if (mysql_num_rows($Result2) == 0) {
print "I didn't get any rows returned with this:<br />'$Query2'";
}

ahmedtheking

9:21 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I attached that in like you said, didnt work! Ive found out that these work:

SELECT * FROM $TableName << I don't want everything!
SELECT * FROM $TableName where pgname LIKE 'pr%' << Ive used that before and still don't want that!

And this works:

SELECT * FROM $TableName where pgname LIKE 'pr_part1'

BUT this:

SELECT * FROM $TableName where pgname LIKE 'pr_part1' AND pgname LIKE 'pr_whywebsite'

AND this:

SELECT * FROM $TableName where pgname LIKE 'pr_part1, pr_whywebsite'

Don't work!

What is up with that?!?!?!

Note, even the mysql_num_rows thingy doesn't work with those!

Nothing shows up... :'(

coopster

9:30 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you turned on error_reporting() [php.net] at the top of your script so you can see any and all messages? Also, try adding an or statement to your query execution to display any error messages upon failure.
$Result2 = mysql_query($Query2, $Link) or 
exit('Error: ' . mysql_errno() . ' : ' . mysql_error());

syber

9:42 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



SELECT * FROM $TableName where pgname LIKE 'pr_part1' AND pgname LIKE 'pr_whywebsite'

I'm not sure what you are trying to do here but this statement could never be TRUE - the column pgname in a single row cannot be both LIKE 'pr_part1' and 'pr_whywebsite', so it will always return FALSE.

Art

ahmedtheking

11:12 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What i'm trying to do is to pull out the info from the DB where pgname = a load of page names! Get what i mean?

CaseyRyan

11:58 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



Sounds like what you want is:

SELECT * FROM $TableName where (pgname = 'pr_part1') OR (pgname = 'pr_whywebsite')

You can programmatically build out the where clause to include all of the pages that you want. Or if you are able to hardcode them, you could do it that way too.

-=casey=-

olwen

12:07 am on Dec 2, 2004 (gmt 0)

10+ Year Member



It looks to me as if you want:

SELECT * FROM $TableName where pgname LIKE 'pr_part1' OR pgname LIKE 'pr_whywebsite'

Confusing AND and OR is a problem that traps most of us at some stage.

ahmedtheking

12:16 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No no no, I want it to select 4 different rows each with different pgname values. This is what ive been trying to do. I asked a few people and they can't spot the problem either!

Is there anyway to do it?

coopster

12:18 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, syber.
Welcome to WebmasterWorld, CaseyRyan.

ahmedtheking, both of these folks, and olwen as well, have offered you sound advice. You have changed your query statements from the original in message #1. I guess we have determined that your issue is not syntax nor code related, but query statement related. Why don't you give a small example of the data in the table and what you are trying to accomplish. It will help get you a much quicker solution. Thanks.

ahmedtheking

12:03 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, what kind of stuff would you like to look at?

ahmedtheking

12:03 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Btw, thank you for everyone helping me out! :D

coopster

12:50 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It looks to me like we are mostly discussing the
pgname
column. So why not show us some example data in that column and some examples of what rows you would like to retrieve based on the data in that column. Don't try to write it in SQL format yet. For example:

This is my sample data in the table. The

pgname
column is CHAR(100), meaning it is a 100 character field and it contains a group of words separated by commas:
pgname 
---------------------------------------------------
cl_data, cl_something, cl_index, sv_index, pr_part1
pr_part1, privacy, sv_index
privacy, accessibility
sv_part1, cl_index

I would like to return only those rows that have words beginning with
'cl_'
, but not those that are
'cl_index'
. How might I go about this?

ahmedtheking

8:24 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry i worded it wrongly! I'll take that into account!

I was trying to do exactly what you described and ive found out why it hasnt work!

Where i have put 'LIKE', i used '=' instead of 'LIKE' and that made it work!

But now the second problem is, what is the opposite of '=' in an sql sense?

bcolflesh

8:25 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what is the opposite of '=' in an sql sense?

<>

ahmedtheking

8:33 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is that it? Im lost! as in does /= (&#8800;) work in sql?