-2

I'am setting up a new wampserver and created a new database with a table named 'users'

apache 2.4.23 & PHP 7.1.10 & mySQL 5.7.14

<?php

    $server = 'localhost';
    $serverUsername = 'root';
    $serverPassword = '';
    $dbName = 'test';

    $connection = mysqli_connect($server,$serverUsername,$serverPassword);
    msqli_select_db($dbName);
    if(!$connection){
        echo 'connection failed to database '.mysqli_connect_error();
    }
    $sql = "SELECT * FROM users";
    $query = mysqli_query($sql);
    while($row = mysqli_fetch_array($query)){
        print_r($row);
    }

?>

my value is really in th data base but nothing appears in the page after running code

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
Mega En
  • 17
  • 5
  • Show us the code you're using to retrieve this data. We can't help you if we don't know what it is you're doing. –  Feb 03 '19 at 14:30
  • 1
    Please don't post code in the comments, instead, edit your question and paste it there. Thanks. Also, here's a link to [the documentation of mysqli_query()](https://secure.php.net/manual/en/mysqli.query.php), you're missing the `$link`(`$connection`) – brombeer Feb 03 '19 at 14:34
  • move your `$dbName` as a 4th parameter when `mysqli_connect` and remove `msqli_select_db` line (becasue it is not necessary and has typo) – Alex Feb 03 '19 at 14:38
  • Fatal error: Uncaught Error: Call to undefined function msqli_select_db() in C:\wamp64\www\mysql\select_mysql.php on line 9 ( ! ) Error: Call to undefined function msqli_select_db() in C:\wamp64\www\mysql\select_mysql.php on line 9 – Mega En Feb 03 '19 at 14:48

1 Answers1

1

Have a look at the comments mentioning the fix

$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';

// FIX 1
// You need to mention the database name as the last argument
$connection = mysqli_connect($server,$serverUsername,$serverPassword, $dbName);
if(!$connection){
    echo 'connection failed to database '.mysqli_connect_error();
}

$sql = "SELECT * FROM users";

// FIX 2
// The first argument should be your mysqli connection
$query = mysqli_query($connection, $sql);

// Check for errors
if (!$query) {
    printf("Error: %s\n", mysqli_error($connection));
    exit();
}

while($row = mysqli_fetch_array($query)){
    print_r($row);
}

?>

Reference for mysqli_connect: https://secure.php.net/manual/en/function.mysqli-connect.php

Reference for mysqli_query: https://secure.php.net/manual/en/mysqli.query.php

Shahlin Ibrahim
  • 1,049
  • 1
  • 10
  • 20
  • Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\mysql\select_mysql.php on line 18 – Mega En Feb 03 '19 at 14:56
  • @MegaEn That means your query is wrong. Does the **users** table exist in the given database? – Shahlin Ibrahim Feb 03 '19 at 14:59
  • Check https://stackoverflow.com/a/14578644/7362396 for how to make mysqli automatically escalate errors in your queries, so you get a better hint. – Tobias K. Feb 03 '19 at 15:04
  • 1
    Adding to @TobiasK. reference, here's another one: https://stackoverflow.com/a/15440076/2736770 which will help you find the error – Shahlin Ibrahim Feb 03 '19 at 15:06
  • @MegaEn I have updated my answer to check for errors, try it and see if there's a better error message – Shahlin Ibrahim Feb 03 '19 at 15:12
  • @ShahlinIbrahim thank you ibrahim it works well can i know what you changed – Mega En Feb 03 '19 at 16:20
  • @MegaEn Other than the fixes I've mentioned in the code, I added the if statement below the `$query` variable that provides you clear error message. [reference](https://stackoverflow.com/a/15440076/2736770) – Shahlin Ibrahim Feb 03 '19 at 16:45