Hi everyone… I am a PHP newbie. Sorry if this is such an elementary question. I am trying to rewrite/update a gallery code using mysqli instead of mysql. I tried to follow an online tutorial. I am getting several errors so how do I rewrite the following bit of code using mysqli? Can someone help me please?
$result =
mysql_query
("SELECT
c.category_name
,c.category_image
,COUNT(p.photo_caption)
FROM
gallery_category as c
LEFT JOIN
gallery_photos as p
ON
p.category_name = c.category_name
GROUP BY
c.category_name");
while ($row = mysql_fetch_array($result)) {
$row[0] = trim($row[0]);
$row[0] = str_replace(" ", "-", $row[0]);
$row[0] = str_replace("'", "%27", $row[0]);
For the most part MySQLi is pretty much the same with the exception of some functions which may have changed names completely or were removed, for what you have above simply changing mysql to mysqli should suffice.
For the most part MySQLi is pretty much the same with the exception of some functions which may have changed names completely or were removed, for what you have above simply changing mysql to mysqli should suffice.
Thank you very much guys… When I change MySQL to MySQLi I get ,
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\\wamp\\www\\viewgallery.php on line 52
and
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\\wamp\\www\\viewgallery.php on line 54
. Line 52 to 54 from the above code is
GROUP BY
c.category_name");
while ($row = mysqli_fetch_array($result)) {
I’ll give you an example in PDO, since I hardly use MySQLi.
try {
// create connection ($dsn holds the connection details)
$pdo = new PDO($dsn, $login, $pass);
// set error handling
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// query the db
$stmt = $pdo->query($sql_without_user_data);
// tell PDO to only fetch the first item of each row
$stmt->setFetchMode(PDO::FETCH_COLUMN, 0);
// fetch data
foreach ($stmt as $item) {
echo $item, '<br>';
}
}
// if there is an error, notify the user
catch (PDOException $exc) {
error_log($exc->getMessage());
echo 'Oops, something went wrong!';
}
// properly close your HTML page
I will certainly try to understand this. Looks like a whole new language to me right now. I am sure I will understand it after I dissect each word of your answer ( may be not) …thank you for taking your time for me.
Hi Everyone, I got past most of the errors. But I am stuck at mysql_result as there are no equivalent for it in mysqli. Can anyone help me? This is the code I am getting the errors at
// Figure out the total number of results in DB:
$total_results = mysql_query("SELECT COUNT(*) FROM gallery_photos WHERE category_name='" . addslashes($cname) . "'");
if (!$total_results) {
die('Could not query:' . mysql_error());
}
$total_results = mysql_result($total_results, 0);
The error that I am getting is
Warning: mysql_result() expects parameter 1 to be resource, object given in C:\wamp\www\viewgallery.php on line 182
Dormilich, thanks you for helping me still…But I am ashamed to admit that I do not know how to use your code to make my code work. May be it is very simple but my knowledge is very basic. I tried but it didn’t work.
I second this too, OOP is professional coding standard and you want to stick to it for good programming practices too. No matter you like it or not, the cold hard truth is that if you are not comfortable with OOP, you wont be able to find a decent job in this industry.