Andrew Crawford
Andrew Crawford

Reputation: 33

Mysql query returns no results

The original problem i had was i was not getting stuff to display, now i have stripped back so it is basic

$host="localhost";
$user="*";
$pass="*";
$db="*";

$con=mysqli_connect($host,$user,$pass,$db) or die ("Could not connect to database " . mysqli_connect_error($con));

$query="SELECT * FROM customers WHERE client='$tmp1'";
$results=mysqli_query($con,$query);
$row_cnt = mysqli_num_rows($results);

printf("Result set has %d rows.\n", $row_cnt);

when i use num rows all i get it saying "Result set has 0 rows."

i ain't sure why i ain't getting any results at all as the query run in phpmyadmin getting the results

Upvotes: 0

Views: 1409

Answers (4)

m1k1o
m1k1o

Reputation: 2364

You didnt set the variable $tmp3 as return of the function. It simply does not exists outside the function.

I noticed now, that you are also calling global variable inside function which does not exists.

You can set the variable in function like refference

function getinfo($tmp1,$tmp2, &$tmp3)    {
    $query = "SELECT * FROM customers WHERE client='{$tmp1}'";

    $results = mysqli_query($GLOBALS["con"], $query);

    while($row = mysqli_fetch_array($results)) {
      $tmp3 = $row['$tmp2'];
    }
}

Then calling:

getinfo("test", "title", $tmp3); 

$tmp3 is filled by that function.

Or, ju just forgot to set Output of the function.

$tmp3 = getinfo("test","title"); 

Upvotes: 0

Gerald Schneider
Gerald Schneider

Reputation: 17797

$con does not exist inside your function. To pull it in the variable scope of the function you need to add this:

function getinfo($tmp1,$tmp2)
{
  global $con;
  // rest of your function.
}

Side note:

this is very, very bad program design. Do you intend to include this file for every input you want have a default value? Get the information from the database one time, store it in variables and just use value="<?=$var?>" in your inputs.

Upvotes: 1

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

Why you include whole code in attribute also your function is returning values so get it into an variable and echo getting var:- try

<?php
include 'function.php'; 
$tmp3 =getinfo("test","title"); 
?>
Title: <input type="text" name="title" value="<?php echo (!empty($tmp3) ? $tmp3 : '');  ?>"><br>
<?php $tmp4 = getinfo("test","background");?>
<option value="test" <?php if(!empty($tmp4) && $tmp4 == 'test'){echo 'selected = "selected"';}?>>Test</option>
<?php mysqli_close($con);?>

Upvotes: 0

Smuuf
Smuuf

Reputation: 6524

You are not returning the value back from your getinfo() function.

It should be $tmp3 = getinfo("test","title"); instead of just calling getinfo("test","title");

Furthermore, you don't need to have such a messy HTML+PHP. Do your including at the beginning of the page. For example:

<?php

include 'function.php';
$value = getinfo('test', 'title');
mysqli_close($con);

?>

Title: <input type="text" name="title" value="<?php echo $value; ?>"><br>

Upvotes: 0

Related Questions