0

I have many simple PHP files with MySQL queries that I need to modify since my webhost migrated from PHP5 -> PHP7. I am pretty much a PHP/MySQL beginner trying to wrap my head around the changes from MySQL to MySQLi.

I've begun reading the PHP docs re: MySQLi but am getting stuck on mysqli_query at the moment.

Here is the MySQLi code I've tried so far:

<?php
$con = mysqli_connect("localhost”,     “my_user","my_password,"my_db”);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$result = mysqli_query("SELECT image, caption
            FROM tbllinkcat, tblimages
        WHERE tbllinkcat.catid = tblimages.catid
            AND tbllinkcat.catid=1;");      

while($row = mysql_fetch_array($result))
  {
    echo $row['image'];
    echo "<br />";
    echo $row['caption'];
    echo "<br />";
  }
mysql_close($con);
?>

I'm pretty sure the mysqli_connect code is working but I get errors on the mysqli_query code (error: Warning: mysqli_query() expects at least 2 parameters.

And I am pretty sure I will get errors on the mysql_fetch_array too once I correct mysqli_query. So for now I was wondering if someone could just show me an example of a mysqli_query that would work for the specific SQL statements in my code above? I will continue reading the PHP docs for MySQli and mysqli_query. Thank you.

obcbeatle
  • 81
  • 1
  • 6

2 Answers2

0
<?php
$con = mysqli_connect("localhost”,“my_user","my_password”,"my_db”);
   //establish database connection
if (!$con)
  {
  die('Could not connect:'.mysqli_error());
  }

$result = mysqli_query($con,"SELECT image,caption FROM tbllinkcat, tblimages WHERE tbllinkcat.catid = tblimages.catid AND tbllinkcat.catid=1");      
//perform sql select query with database connection
while($row = mysqli_fetch_array($result))
  {
    echo $row['image'];
    echo "<br />";
    echo $row['caption'];
    echo "<br />";
  }
mysqli_close($con);
?>

For More Info read :-https://www.php.net/manual/en/mysqli.query.php

Mohit Kumar
  • 952
  • 2
  • 7
  • 18
0

mysqli_query needs two parameters:

  • Your database connection
  • A query

That will result in this:

$result = mysqli_query($con, "SELECT image, caption FROM tbllinkcat, tblimages WHERE tbllinkcat.catid = tblimages.catid AND tbllinkcat.catid=1;");   

You also have some weird looking quotation marks in your connect function, correct them:

$con = mysqli_connect("localhost", "my_user", "my_password", "my_db");

To fetch the array, you would use:

while($row = mysqli_fetch_assoc($result))
{
    echo $row['image'];
    echo "<br />";
    echo $row['caption'];
    echo "<br />";
}

And finally, to close the connection:

mysqli_close($con);

Note: Just so you know, when using queries with user input, please use prepared statements and bind_param. This will prevent SQL injection attacks. I will show and example below.

$stmt = $db->prepare("SELECT * FROM users WHERE username = ?");

$username = $_POST['username'];

$stmt->bind_param('s', $username);

$result = $stmt->execute();
rpm192
  • 2,630
  • 3
  • 20
  • 38