Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page Using php variables in MySQL queries
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
Junior Member
idhrahil is on a distinguished road
 
Posts: 7
Join Date: Jun 2012
Default Using php variables in MySQL queries - 06-06-2012, 03:52 PM

I am trying to run a script that produces a name from a query to a MySQL database:

PHP Code:
<?php

$sessionid 
'ne8295jskeuv03582je8ao2lf99sm3ng';

$query =
"SELECT users.First_name
FROM sessions
INNER JOIN users
ON sessions.UserID = users.UserID
WHERE sessions.SessionID = $sessionid "
;

$con mysql_connect ("mysql7.000webhost.com""DELETED BY MODERATOR""DELETED BY MODERATOR");
$db mysql_select_db("DELETED BY MODERATOR",$con);

$result mysql_query ($query);

while(
$row mysql_fetch_array($result))
  {
  echo 
$row['First_name'];
  }

?>
It involves using a php variable within the SQL query. When I replace this with the string itself it works fine, but when I try to do it with the variable in its place it says:
Quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a5938807/public_html/test.php on line 25
What's going on? How do I put a variable into a query so that it is picked up properly as a string?

Last edited by d3iti; 06-06-2012 at 05:19 PM. Reason: Private data (user and password BD) should not be written in the forum.
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
dx4's Avatar
dx4 dx4 is offline
Member
dx4 is on a distinguished road
 
Posts: 95
Join Date: Mar 2012
Send a message via Skype™ to dx4
Default 06-06-2012, 04:58 PM

Try assigning the query variable in a single line.


And when everything else fail, try reading the manual.
Reply With Quote
(#3 (permalink))
Old
Junior Member
idhrahil is on a distinguished road
 
Posts: 7
Join Date: Jun 2012
Default 06-06-2012, 05:58 PM

Nope, still doesn't work. And that wouldn't explain why it works when I put in the data myself. It's only when I try to use the variable $sessionid instead of directly inputting a string that it doesn't work.
Reply With Quote
(#4 (permalink))
Old
d3iti's Avatar
Super Moderator
d3iti is on a distinguished road
 
Posts: 6,620
Join Date: Jul 2009
Location: Spain
Default 06-06-2012, 06:09 PM

If the problem is with the name of the session, you can try to put single quotes in the query to the variable $sessionid:
PHP Code:
$query =
"SELECT users.First_name
FROM sessions
INNER JOIN users
ON sessions.UserID = users.UserID
WHERE sessions.SessionID = '$sessionid' "



Recuerda realizar copias de seguridad de tus sitios web. Si este mensaje te ayudó puedes pulsar sobre el botón karma
Reply With Quote
(#5 (permalink))
Old
Junior Member
idhrahil is on a distinguished road
 
Posts: 7
Join Date: Jun 2012
Default 06-06-2012, 06:30 PM

That seems to work - thanks!
Reply With Quote
(#6 (permalink))
Old
d3iti's Avatar
Super Moderator
d3iti is on a distinguished road
 
Posts: 6,620
Join Date: Jul 2009
Location: Spain
Default 06-07-2012, 06:37 AM

No problem.


Recuerda realizar copias de seguridad de tus sitios web. Si este mensaje te ayudó puedes pulsar sobre el botón karma
Reply With Quote
Reply

Tags
mysql, php

Thread Tools
Display Modes

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

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




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com