Wednesday, September 18, 2019

Fetch only one row in PHP/MySQL











simple question here.




I have a SELECT query



SELECT FROM friendzone WHERE ID = '$editID'"


I am sure this is going to give me only one row as result, cause ID's can't be duplicate in my DB.



How can I access the column values?




$row = mysql_fetch_array($query);


I think this is useless since I don't have to make any array.. I have only one row!



If I don't put it into a While cicle, and try to do e.g.



.$row['ID'].



I get:



mysql_fetch_array() expects parameter 1 to be resource, boolean given


Thanks in advance everyone.


Answer




Please, don't use mysql_* functions in new
code
. They are no longer maintained and the

deprecation process has begun on it. See the
red box? Learn about prepared
statements
instead, and use
PDO or MySQLi - this
article
will help you decide which. If you choose
PDO, here is a good tutorial.




Try with:




$query = mysql_query("SELECT * FROM friendzone WHERE ID = '$editID'");
$row = mysql_fetch_array($query);

print_r($row);


MySQLi code:



$conn = new mysqli('host', 'username', 'password', 'database');
$stmt = $conn->prepare("SELECT * FROM friendzone WHERE ID = ?");

$stmt->bind_param("s", $editID);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

print_r($row);

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...