php - Cannot select from table, but I can insert and create? -


i have extremely weird issue mysql. can insert into, , create tables. cannot select , not display error when try, echos line "error: "

getdiff("validtable");  function getdiff($regnr) {     global $servername, $username, $password, $dbname;     // create connection     $conn = new mysqli($servername, $username, $password, $dbname);     // check connection     if ($conn->connect_error) {         die("connection failed: " . $conn->connect_error);     }     //todo     $sql = "select * $regnr order id limit 1";     $result = $conn->query($sql);      if ($result === true) {         echo "done";     } else {         $error = $result->error;         echo "error: " . $error;     }     $conn->close(); } 

but when insert db using this

$query = "create table if not exists $regnr ( `id` mediumint not null auto_increment, `mail` int not null , `price` int not null , `views` int not null , `the_date` date not null , `time` varchar(5) not null, primary key (id) )"; runquery($query);  function runquery($todo) {    global $servername, $username, $password, $dbname;    // create connection    $conn = new mysqli($servername, $username, $password, $dbname);    // check connection    if ($conn->connect_error) {        die("connection failed: " . $conn->connect_error);    }    //todo    $sql = $todo;     if ($conn->query($sql) === true) {        echo "done";    } else {        echo "error: " . $conn->error;    }    $conn->close(); } 

it works fine , dandy. have done wrong?! driving me crazy!

it shows "error" because query succeeds , returns mysql object not equal true , therefore not echo done

see (http://php.net/manual/en/mysqli.query.php)

returns false on failure. successful select, show, describe or explain queries mysqli_query() return mysqli_result object. other successful queries mysqli_query() return true.

change code code below make work:

   if ($conn->query($sql) === false) {        echo "error: " . $conn->error;    } else {        echo "done";    } 

basically turning if around such checks false or not false.

most want store result in variable like:

   if ($result = $conn->query($sql) === false) { 

and later on use $row = $result->fetch_array() or $row = $result_fetch_assoc() data.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -