Here is my problem,
Not using prepared statements I can do it just fine, for example,
$qry = "SELECT * FROM accounts WHERE email = '$email'";
$result = mysql_query($qry);
$account = mysql_fetch_assoc($result);
echo '<p>Welcome <strong>' . $account['username'] . '</strong>, Have a good day! And dont for开发者_StackOverflow社区got your id ' . $account['id'] . '.</p>';
Considering an email does match a row on the mysql database, then I can with ease echo any other column where the email matches by simply doing $account['gender'], $account['age'] for example.
I am having alot of trouble doing it OO, here is my attempt;
$q = $dbc -> prepare ("SELECT * FROM accounts WHERE email = ?");
$q -> bind_param ('s', $email);
$q -> execute();
$q -> bind_result();
$info = $q -> fetch();
echo '<p>Welcome ' . $info['username'] . '.</p>';
Doing it with the first method I can display any information from any column where the email matches for that row, I switched to prepared statements for security, but I am thinking of switching back with the hassle it is causing!
bind_result
takes parameters. You pass it the variables you want it to set, then you call fetch
.
$q->bind_result($username);
$q->fetch();
echo $username;
For this to work, you need to change SELECT *
to the fields you want, ie SELECT username
.
If you still need to use SELECT *
, you can do this:
$q->execute();
$r = $q->get_result();
while($row = $r->fetch_array(MYSQLI_ASSOC)){
}
Good old MySQL extension does not support prepared statements so you must have switched to another extension you don't mention. If it happens to be mysqli, you're out of luck: it only supports associative arrays when you don't use prepared statements.
My advise is to try out PDO. The MySQL driver is stable and it has a great API you can reuse for other DBMS engines.
精彩评论