InzeBielderman
InzeBielderman

Reputation: 35

PHP - Test if there's a MySQLi output

I want to have a php script that tests if there's a mysql output, currently I have the following code:

$mysqli = new mysqli('localhost', 'root', NULL, 'forum');
$query = "SELECT id,titel,auteur,datum FROM topics WHERE categorieid=$categorieid ORDER by id DESC";

To test for a result I have no working code, so it wouldn't help posting the rest here

I want an if statement, if the query sends back a result echo this, else echo this

Who can help me with this? Thanks!

Reminder: It has to be MySQLi, using MySQL is 'outdated' as PHP calls it

EDIT:

if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($id,$titel,$auteur,$datum);
$stmt->close();
}

Upvotes: 0

Views: 107

Answers (2)

Vipin Kumar Soni
Vipin Kumar Soni

Reputation: 834

$mysqli = new mysqli('localhost', 'root', '', 'forum');
$query = "SELECT id,titel,auteur,datum FROM topics WHERE categorieid='$categorieid' ORDER by id DESC";

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

if ($result = $mysqli->query($query)) {
    printf("Select returned %d rows.\n", $result->num_rows);
    $result->close();
} else {
    printf("Select returned no rows.\n");
}

Upvotes: 1

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

you need to try like

$mysqli = new mysqli('localhost', 'root', '', 'forum');
$query = "SELECT id,titel,auteur,datum FROM topics WHERE categorieid='$categorieid' ORDER by id DESC";

query variable should be quoted.

For more :- http://www.php.net/manual/en/mysqli-stmt.execute.php

Upvotes: 0

Related Questions