-1

I get a Call to undefined function mysql_query() error in my code, i don't know what i do wrong. Can somebody help me?

 <?php

 $sqlcheck = "SELECT * FROM product";
 $result = mysql_query($conn, $sqlcheck);    <====== 'Xampp says the error is in this line'
 $resultCheck = mysql_num_rows($result);

 if ($resultCheck > 0) {
 while ($row = mysqli_fetch_assoc($result)) {
   echo $row . "<br>";
 }
 }

 ?>

I checked the connection with a cath error, and the connection works so nothing wrong with that. $conn is defined in another file which i did connect to my php file with this code:

include_once('includes/dbh.inc.php');

So i hope somebody can help me. Thanks

Hakfo
  • 61
  • 1
  • 4

2 Answers2

0

If you are using >= PHP 7.0 then this function has been removed. Please use following instead:

mysqli_query
Abbas Akhundov
  • 562
  • 6
  • 12
  • thanks! i'm one step further, but now i get another error: mysqli_query() expects parameter 1 to be mysqli, do you know what that means? – Hakfo Sep 29 '18 at 19:15
  • @Hakfo Yes, your **$conn** shold also use mysql**i** when initiated. Basically convert all of your code to mysqli instead of mysql and you should be fine. Also if you found my answer helpful then please select it as the correct one. – Abbas Akhundov Sep 29 '18 at 19:17
  • $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName); It already is – Hakfo Sep 29 '18 at 19:18
  • @Hakfo Yes, that is the correct code. You have the described error using this code? – Abbas Akhundov Sep 29 '18 at 19:21
  • no, thats the strange part, only the mysqli_query() expects parameter 1 to be mysqli, i never had this before, strange – Hakfo Sep 29 '18 at 19:22
  • @Hakfo Then there is a problem with mysqli_connect inputs. Please check that all inputs are correct. Basically this error means that it cannot connect to the DB and mysqli_connect function returns **FALSE** (Boolean). – Abbas Akhundov Sep 29 '18 at 19:25
  • While mysqli is nice, I still strongly recommend switching to the newer PDO. – trollboy Sep 29 '18 at 19:29
  • 1
    i found the problem my $conn and $sqlcheck wereturned in the $result variable. Thanks for the help guys! – Hakfo Sep 29 '18 at 19:31
  • @trollboy I would strongly suggest mysqli over PDO for beginners. PDO might be better. However, it is a little bit complicated for a start point. – Abbas Akhundov Sep 29 '18 at 19:32
-1

It sounds like you haven't included LibMySQL in your PHP install. This is actually a good thing, as LibMySQL is deprecated. I strongly recommend using PDO to do your db queries. PHP's PDO page should help you understand how to use PDO. If you have a legitimate requirement for using the now deprecated LibMySQL you may have to run an older version of PHP and ensure support for LibMySQL is installed.

trollboy
  • 133
  • 7