Jump to content

php acting odd?


aragon_202_

Recommended Posts

Hi, I’m reasonable new to php coding, done my fair bit of "hacking" scripts together before hand but I have recently took up doing things from scratch.

 

I'm attempting to build a simple app to count the rows in a MySQL database, randomise between the returned number and 1 and display the result. simple.

 

 

<?php
$host= "localhost";
$user="username";
$password="password";
$dbname="dbname";
$entries="SELECT COUNT(*) FROM Review";
$cxn = mysqli_connect($host,$user,$password,$dbname)  
    or die ("connection failed lol");
$result = mysqli_query($cxn,$entries) 
    or die ("couldent do it");
$result = $result + 1;
echo "$result";
?>

 

the results of which was a fatal error in trying to dispaly the result, which was understandable. so i added a +1 and then displayed the result, the answer of which always displays 2 meaning it is returning 1. where as phpmyAdmin returns 3 (correct answer) meaning the script should display 4 (due to the +1)

 

any ideas on how to fix this?

 

Link to comment
https://forums.phpfreaks.com/topic/105261-php-acting-odd/
Share on other sites

how about trying mysql_num_rows to return how many entries in the database,

 

 

very simply put:


$entries="SELECT * FROM Review";

$result = mysql_query($entries)    or die ("couldn't do it");

$numresult = mysql_num_rows($result);

echo rand(1,$numresult);

 

i noticed you say mysqli instead of mysql in above code...is that a typo or some other form of coding?

 

later

vurentjie

 

BUt the above solution is way better,

Link to comment
https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-538977
Share on other sites

after using the second example iv got the random number work  ;D but iv got a question as to how to use a variable with an SQL query? (code below) to then get that record and add it to an array :)

 

$name="SELECT * FROM 'Review` WHERE `id` = $numresult";
$nameresult[] = mysqli_query($cxn,$name)    
    or die ("couldent get  name");
echo $nameresult[0];

Link to comment
https://forums.phpfreaks.com/topic/105261-php-acting-odd/#findComment-539039
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.