Jump to content

Proper Syntax


tcorbeil

Recommended Posts

I am having trouble with this code:

 

$database = "oilers"

$query="SELECT * FROM '$database' WHERE Page = '$pagepath'";

 

Even though I know I have a table in my database called oilers, it doesn't seem to work..

I'm thinking it is just my syntax using the variable $database.. can someone let me know if there is something wrong?

 

Thanks everyone.

 

T.

Link to comment
https://forums.phpfreaks.com/topic/44728-proper-syntax/
Share on other sites

Hmmm. Try this:

 

$query="SELECT * FROM '" . $database . "' WHERE Page = '" . $pagepath . "'";

 

Variables inside of single quotes have been giving me troubles today.

 

With my frustration today anyway, echo('Are you happy?: $happy'); would output:

Are you happy?: $happy

 

With double quotes - echo("Are you happy?: $happy");

Are you happy?: Yes!

 

Link to comment
https://forums.phpfreaks.com/topic/44728-proper-syntax/#findComment-217152
Share on other sites

Ok maybe I'm on the wrong track...

 

I'm getting this error:

 

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/tcorbeil/public_html/Entertainment/Tim_123/Tim_123.php on line 897

 

with this code:

 

 

$database = "Tim_123";

$query="SELECT * FROM $database WHERE Page = '".$pagepath."'";

$result=mysql_query($query);

$num=mysql_numrows($result);

 

it seems the error would have to do with the numrow..  I use $num=mysql_numrows($result); to count the amount of occurences in the search...

 

T.

Link to comment
https://forums.phpfreaks.com/topic/44728-proper-syntax/#findComment-217170
Share on other sites

Firstly, get in the habbit of checking your queries actually work before trying to use them, the code you posted is extremely error prone.

 

<?php

 // connect to db.

 $database = "Tim_123";
 $query = "SELECT * FROM $database WHERE Page = '$pagepath';";
 if ($result = mysql_query($query)) {
   if (mysql_num_rows($result)) {
     while ($row = mysql_fetch_assoc($result)) {
       print_r($row);
     }
   } else {
     echo "No results found.";
   }
 } else {
   echo "Query failed. $query<br />" . mysql_error();
 }

?>

Secondly... where is $pagepath defined?

Link to comment
https://forums.phpfreaks.com/topic/44728-proper-syntax/#findComment-217177
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.