Jump to content

mysql_query with variables


niche

Recommended Posts

Here's the script what's not working:

$result = mysql_query("SELECT * FROM selections WHERE timestamp = $ts ") or die(mysql_error());$rows = mysql_num_rows($result);echo var_dump($rows);

$ts is an INT.I've echoed everything. There are no error messages. What am I doing wrong please?

Link to comment
Share on other sites

You need to add the . between the variable and the rest of the query, like so:$result = mysql_query("SELECT * FROM selections WHERE timestamp =\"" . $ts . "\"") or die(mysql_error());

Link to comment
Share on other sites

I think I get what you're driving at except when I copy pasted the change it didn't work.Is that literally how that script should be written?

Link to comment
Share on other sites

All the variables checkout (with echo).Any other thoughts?

Link to comment
Share on other sites

All the variables checkout (with echo).Any other thoughts?
does it actually correlate with a timestamp in the database?
Link to comment
Share on other sites

Yes $ts = 1277501506 and there's and the last record in the database has a timestamp of 1277501506.

Link to comment
Share on other sites

actually, maybe you should be using the comparison (==) operator instead. Another thing I can think of is trying to use the literal value 1277501506 just to see what happens.

Link to comment
Share on other sites

Good ideas.Except the comparison didn't work. Do you know the function for turning an INT into a string (I can't find it)?Thanks

Link to comment
Share on other sites

You need to add the . between the variable and the rest of the query, like so:$result = mysql_query("SELECT * FROM selections WHERE timestamp =\"" . $ts . "\"") or die(mysql_error());
No, you don't, because using " " instead of ' ' means that variables do get parsed inside the string.
actually, maybe you should be using the comparison (==) operator instead. Another thing I can think of is trying to use the literal value 1277501506 just to see what happens.
No, MySQL doesn't work like that.What does the code return, and what are you expecting it to return? May we see an output? May we see the rest of the code?
Link to comment
Share on other sites

The complete code is :

//session_start();$ts = $_SESSION['timestamp'];echo '</br>';echo $ts;include_once "connect_to_mysql.php";$result = mysql_query("SELECT * FROM selections WHERE timestamp =  $ts") or die(mysql_error());echo '</br>';$rows = mysql_num_rows($result);echo var_dump($rows);

As I said, $ts is an INT.$ts = 1277502815 and the timestamp of the last record is 1277502815The var dump = int(1) and there are no error messages.Thanks

Link to comment
Share on other sites

I ask again. What is the script supposed to do? Also, why is session_start() commented out?

Link to comment
Share on other sites

When I use session_start() I get this error message:Notice: A session had already been started - ignoring session_start() in C:\wamp\www\selected.php on line 3I assumed that's because the script is triggered by an include_once in another script (that also used a start_session() ).Is that assumption a fact? I was planning to make it a new topic.Now the stupid part. I copy pasted $rows = mysql_num_rows($result); thinking it was $rows = mysql_fetch_array( $result );That was the problem.I suppose no error message really meant no error in this case! I apologize for the thought twister. I caught my mistake when 23.12.2012 asked for the whole script.Also, the purpose of the script is simply to select the record with the correct timestamp.Please let me know about the start_session and confirm that if $ts was a string would I need to write the script WHERE timestamp = '$ts' ? Thanks,Niche

Link to comment
Share on other sites

You only need to start a session once per request. Including a file doesn't make another request, it adds more code to the current one. You only need to quote a value in the query if the database field is a string.

Link to comment
Share on other sites

Thanks for everyone's help especially real_illusions, thescientist, 23.12.2012, and justsomeguy.Niche

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...