Jeff Lee
Jeff Lee

Reputation: 11

ERROR : fetching data from mysql and showing in on the web

<?php

  $user = 'root';
  $password = 'root';
  $db = 'urls';
  $host = 'localhost';
  $port = 8889;

  $link = mysqli_init();
  $success = mysqli_real_connect(
     $link, 
     $host, 
     $user, 
     $password, 
     $db,
     $port
  );

  //echo $success;

  $query = "SELECT id, title, url, votes FROM link ORDER BY id LIMIT 3";

  $result = mysqli_query($success, $query);

  $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  printf ("%s (%s)\n", $row["title"], $row["url"]);

  /* free result set */
  mysqli_free_result($result);

  /* close connection */
  mysqli_close($link);

?> 

So this is the code i wrote. Obviously, the purpose of this is to fetch latest datas from my mysql server and print it out. However, when i run this code on my mac(I'm using MAMP), IT ONLY SHOW '0'. What is wrong with this?

Upvotes: 0

Views: 621

Answers (2)

VolkerK
VolkerK

Reputation: 96159

Try it with a bit more error handling

<?php
$user = 'root';
$password = 'root';
$db = 'urls';
$host = 'localhost';
$port = 8889;

echo date('Y-m-d H:i:s', filemtime(__FILE__)), "\r\n";

$link = mysqli_init();
if ( !mysqli_real_connect($link, $host, $user, $password, $db, $port) ) {
    trigger_error('connect failed', E_USER_ERROR);
}
else {
    $query = "SELECT id, title, url, votes FROM link ORDER BY id LIMIT 3";
    $result = mysqli_query($link, $query); // <- you have to pass the connection resource/object here
    if ( !$result ) {
        trigger_error('query failed', E_USER_ERROR);
    }
    else {
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        if ( !$row ) {
            echo 'no results';
        }
        else {
            do {
                printf ("%s (%s)\n", $row["title"], $row["url"]);
            } while($row = mysqli_fetch_array($result, MYSQLI_ASSOC));
        }
        /* free result set */
        mysqli_free_result($result);
    }
    /* close connection */
    mysqli_close($link);
}

( the echo date... line is for checking that you're looking at the output of your latest and greatest script version ;-) )

Upvotes: 1

Hanky Panky
Hanky Panky

Reputation: 46900

mysqli_real_connect returns a boolean, it does not return a MySQLi resource. Hence you have to run the query on $link and not on $success

$result = mysqli_query($success, $query);
                       ^

Should be

$result = mysqli_query($link, $query);

Without any error checking in place it becomes like a guessing game, please consider error checking whether the connection resulted in an error or the query itself, that will help you a long way.

Also note that you have to either fetch all the rows together in one call (using a different function as to what you use now) or you have to loop through the result to fetch all the rows, with your current code you will get only 1 row at max even when the issue is resolved.

On another note, why are you not using the simpler mysqli_connect option and using this 2 step connection without any special flags being used?

Upvotes: 2

Related Questions