-1

I am trying to print out the "branch" table from one of my mysql databases, that is named dreamhome. This is the code I have so far, could you tell me what I need to put inside of my php file so that it prints out queries from this database without the error I am getting?

I've already successfully connected and I have tried saving the query into a string and then using the my sql query function to query the database.

<?php
$servername = "localhost";
$username = "root";
$password = "";

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}
 echo "Connected successfully";

$query = "SELECT * FROM branch";
$result = mysql_query($query);

while ($value = mysql_fetch_array($result)){
 echo $value ('street');
}
?>

Below contains the error that I am getting.

Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\myprojectweb\index.php:16 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myprojectweb\index.php on line 16

prosoitos
  • 5,921
  • 5
  • 27
  • 39
  • mysql and mysqli are different, don't mix the functions, also you started using OOP (Object oriented style) form, you should stick to that. https://www.php.net/manual/en/mysqli.query.php – antoni Nov 07 '19 at 01:35

1 Answers1

0

You can't mix mysql and mysqli,

This is how to successfully connect to a DB and perform a query using Mysqli OOP style: (from this official page: https://www.php.net/manual/en/mysqli.query.php & https://www.php.net/manual/en/mysqli-stmt.fetch.php)

In your case:

(edited after @funk forty niner comment)

<?php
$servername = "localhost"; 
$username = "root"; 
$password = "";
$databasename = "db_name";

$mysqli = new mysqli($servername, $username, $password, $databasename);

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = "SELECT * FROM branch";
if ($stmt = $mysqli->query($query)) {
    $branches = $stmt->fetch_all(MYSQLI_ASSOC);

    print_r($branches);
}
antoni
  • 4,295
  • 30
  • 39
  • This won't work. You can't query without a database parameter. Plus, the prepare/execute is redundant. `query()` is enough. – Funk Forty Niner Nov 07 '19 at 02:01
  • [Your first method](https://stackoverflow.com/revisions/58740782/1) was correct having 4 parameters. – Funk Forty Niner Nov 07 '19 at 02:08
  • @FunkFortyNiner, I edited my incorrect answer thanks for the check. For information the 4th param for connection is optional: https://www.php.net/manual/en/mysqli.construct.php, (but you would have to select db after connection then, which I forgot while copy pasting). Also I don't think the OP was intending to mix both mysqli and mysql but just confused how to use them - and as per the title of the question this does not look like a duplicate of can I mix mysqli and mysql. – antoni Nov 07 '19 at 03:49
  • sorry if I offended you mate, it's not a big deal for me - more for the OP who is new. – antoni Nov 07 '19 at 03:53
  • Your code is suggesting a very bad practice of displaying errors manually. Please don't do that. Enable automatic exceptions instead. – Dharman Nov 07 '19 at 07:27
  • Agreed. this is a connection example from official page https://www.php.net/manual/en/mysqli.query.php – antoni Nov 07 '19 at 07:55