Reputation: 341
I have the following PHP code, which is supposed to get the "first" and "last" values of the column where the "id" matches what is in the URL. For instance, when the URL is ../Profile?id=1
, it would say First: Bob Last: Doe
, because Bob Doe
is attached to the id of 1
.
I do this with the following code:
session_start();
$id = $_GET['search'];
$dbhandle = mysql_connect("localhost", "SocialAdmin", "******")
or die("Unable to connect to MySQL");
$selected = mysql_select_db("socialdonuttesting",$dbhandle)
or die("Could not select database");
$result = mysql_query("SELECT * FROM users WHERE id = '$id'");
$first = $result['first'];
$last = $result['last'];
echo "First: $first Last: $last";
But for some reason, this is just displaying First: Last:
when I go to ../Profile?id=1
. Does anyone know why?
Upvotes: 0
Views: 90
Reputation: 820
$result
is a resource, you have to use mysql_fetch_*
functions. In addittion, I don't recommend you to use * in your SQL query (for example, if you change table structure in the future, the results will be messed up.
Upvotes: 0
Reputation: 2499
You need http://php.net/manual/en/function.mysql-fetch-row.php
However, drop the mysql_ usage and go with PDO.
Also, you are vulnerable to sql injection in your example.
Upvotes: 1
Reputation: 37233
you are not fetching you query
try this
session_start();
$id = $_GET['search'];
$dbhandle = mysql_connect("localhost", "SocialAdmin", "******")
or die("Unable to connect to MySQL");
$selected = mysql_select_db("socialdonuttesting",$dbhandle)
or die("Could not select database");
$result = mysql_query("SELECT * FROM users WHERE id = '$id'");
while($row = mysql_fetch_array($result)){
$first = $row['first'];
$last = $row['last'];
echo "First: ". $first. " Last: ". $last ."</ br>"; }
Upvotes: 0
Reputation: 324620
You are not fetching the result.
$row = mysql_fetch_assoc($result);
echo $row['first'];
echo $row['last'];
Upvotes: 3