Please see I already checked old threads but did not help.
Here is my code, seems ok but gives error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
code:
function getPnr()
{
$con = mysqli_connect('127.0.0.1', 'root', '', 'safari');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$pnr = mt_rand(1111111111, 99999999999);
$result = mysqli_query($con,"SELECT pnr from tbl_user where pnr = '".$pnr."'");
if(mysqli_num_rows($result)>0)
echo "emtpy";//getPnr();
else
echo $pnr;
}
what's wrong here?
UPDATED code:
function getPnr()
{
$con = mysqli_connect('127.0.0.1', 'root', '', 'safari');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$pnr = mt_rand(1111111111, 99999999999);
$result = mysqli_query($con,"SELECT user_pnr from tbl_user where user_pnr = '".$pnr."'") or die(mysqli_error($con);
if(mysqli_num_rows($result)>0)
echo "emtpy";//getPnr();
else
echo $pnr;
return;
}
new error:
Parse error: syntax error, unexpected ';' in F:\wamp\www\safari\REST_APIs_for_RedBus\main.php on line 104
This means your sql query failed to execute. Try to add error handling with your code then you will get clear error sql message
$result = mysqli_query($con,"SELECT pnr from tbl_user where pnr = '".$pnr."'") or die(mysqli_error($con));
Related
I am still learning PHP and I'm trying to get around this error I'm getting.
As per this link my code is correct, but this is my code and this is the error i'm receiving:
$con = mysqli_connect("IP","username","passowrd","dbname");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
function get_demos(){
$result = mysqli_query($con,"SELECT * FROM demos");
if(!$result)
{
die("Invalid query ".mysqli_error($con));
}
return $result;
}
get_demos();
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/content/83/11483383/html/php/db.php on line 10
Warning: mysqli_error() expects parameter 1 to be mysqli, null given
in /home/content/83/11483383/html/php/db.php on line 13 Invalid query
What am I doing wrong?
Thanks.
You should try like,
$con = mysqli_connect("IP","username","passowrd","dbname");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
function get_demos($con){
$result = mysqli_query($con,"SELECT * FROM demos");
if(!$result)
{
die("Invalid query ".mysqli_error($con));
}
return $result;
}
get_demos($con);
You are not declaring that a variable $con is already set.
Try this one
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'databse_name_here') or die ('Failed to connect to MySQL: ' . mysqli_connect_error());
function get_demos($con){
$result = mysqli_query($con,"SELECT * FROM users");
if(!$result)
{
die("Invalid query ".mysqli_error($con));
}
return $result;
}
get_demos($con);
You have to pass your connection to your function. if you don't want to do it every time you can use singleton pattern to always have it in scope.
class DBCon {
private static $_instance = null;
private function __construct() {
$_instance = mysqli_connect("IP","username","passowrd","dbname");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
public static function get() {
if(is_null(self::$_instance)) {
self::$_instance = new DBCon();
}
return self::$_instance;
}
}
and use it in your code :
function get_demos(){
$result = mysqli_query(DBCon::get(),"SELECT * FROM demos");
if(!$result)
{
die("Invalid query ".mysqli_error(DBCon::get()));
}
return $result;
}
I figured out the solution after putting the question:
$con = mysqli_connect("IP","username","passowrd","dbname");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
function get_demos(){
global $con;
$result = mysqli_query($con,"SELECT * FROM demos");
if(!$result)
{
die("Invalid query ".mysqli_error($con));
}
return $result;
}
get_demos();
By adding global $con.
Actually, I connected to 3 databases in different server, but I got problem in mysqli_query.
sometimes it worked fluently without any error, but sometimes it showed "PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result"..
I don't know what happen, it ocurred sometimes.
here is my connection :
connection.php
<?php
$DatabaseName1 = "db_name1";
$DbHostName1 = "host1";
$DbUserName1 = "username";
$DbPassWord1 = "password";
$DatabaseName2q = "db_name2";
$DbHostName2 = "host2";
$DbUserName2 = "username2";
$DbPassWord2 = "password2";
$DatabaseName3q = "db_name3";
$DbHostName3 = "host3";
$DbUserName3 = "username3";
$DbPassWord3 = "password3";
$mysqli1 = mysqli_connect("$DbHostName1", "$DbUserName1", "$DbPassWord1", "$DatabaseName1");
$mysqli2 = mysqli_connect("$DbHostName2", "$DbUserName2", "$DbPassWord2", "$DatabaseName2q");
$mysqli3 = mysqli_connect("$DbHostName3", "$DbUserName3", "$DbPassWord3", "$DatabaseName3q");
$server[1] = $mysqli1;
$server[2] = $mysqli2;
$server[3] = $mysqli3;
$count_db = 3;
if(!$mysqli1){
echo "error to connect server 1st";
die();
}
if(!$mysqli2){
echo "error to connect server 2nd";
die();
}
if(!$mysqli3){
echo "error to connect server 3rd";
die();
}
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
task1.php
<?php
include("connection.php")
for($i=1;$i<=$count_db;$i++){
$conn = $server[$i];
$array = mysqli_fetch_array(mysqli_query($conn,"select * from `table`"));
$task = $array["field"];
}
?>
anyone guys can help?
Do it like this
for($i=1;$i<=$count_db;$i++){
$conn = $server[$i];
$resultset = mysqli_query($conn,"select * from `table`")
if(!$resultset){
//do something
continue; //if you don't want to break it
}
$array = mysqli_fetch_array($resultset);
$task = $array["field"];
}
Here's the code:
<?php
$sql = mysql_query($db, "CALL selectproducts()");
if( $sql === FALSE ) {
die('Query failed returning error: '. mysql_error());
} else {
while($row=mysql_fetch_array($sql))
{
$id=$row['prodname'];
$name=$row['proddescription'];
$desc=$row['prodsupplier'];
$supp=$row['proddate'];
$date=$row['prodprice'];
$qtyleft=$row['prodquantity'];
Getting this Error:
Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\inventory\tableedit.php on line 166
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\inventory\tableedit.php on line 170
Why is it has errors when in fact i have no parameters in call procedure?
Should be:
mysql_query("CALL selectproducts()", $db);
Documentation
Note that the mysql_ functions are now depreciated.
Try this method:
<?php
$link = mysqli_init();
mysqli_options($link, MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0");
mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
mysqli_real_connect($link, $hostname, $username, $password, $dbName);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "CALL simpleproc()";
if (mysqli_real_query($link,$query)) {
if ($result2 = mysqli_store_result($link)) {
while ($row = mysqli_fetch_assoc($result2)) {
$q = $row["login"];
echo $q;
}
}
}
mysqli_close($link);
?>
I do believe you're getting your mysql_query arguments getting mixed up, where was $db is the second parameter, and the first is the MySQL query to be executed.
Although furthermore, you're probably better off using mysqli instead for future proofing:
<?php
$mysqli = new mysqli('username', 'username', 'password', 'database' );
$result = $mysqli->query("CALL selectproducts()");
if( !$result ) {
die('Query failed returning error: '. $mysqli->connect_errno );
} else {
while( $row = $result->fetch_array(MYSQLI_ASSOC)) {
$id = $row['prodname'];
$name = $row['proddescription'];
$desc = $row['prodsupplier'];
$supp = $row['proddate'];
$date = $row['prodprice'];
$qtyleft = $row['prodquantity'];
}
}
?>
From checking the mysqli PHP Docs for calling stored procedure:
<?php
/**
* Prepare Stored Procedure
**/
if (!($result = $mysqli->prepare("CALL selectproducts()"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$result->execute()) {
echo "Execute failed: (" . $result->errno . ") " . $result->error;
}
/**
* Iterate through each result
**/
do {
if ($res = $result->get_result()) {
printf("---\n");
var_dump(mysqli_fetch_all($res));
mysqli_free_result($res);
} else {
if ($result->errno) {
echo "Store failed: (" . $result->errno . ") " . $result->error;
}
}
} while ($result->more_results() && $result->next_result());
?>
<?php
$id = $_POST["id"];
$id2 = $_POST["id2"];
$db = new mysqli('localhost', '***', '***', '***');
if (mysqli_connect_errno() == 0) {
$sql = "SELECT * FROM chat_message WHERE id > " . $id . " AND id <= " . $id2 . " ORDER BY id";
$result = $db->query($sql);
while ($msg = $result->fetch_object()) {
?>
<div>...</div>
<?php
}
}
$db->close();
?>
I am getting this error:
Call to a member function fetch_object() on a non-object in /var/www/template/loadchat.php on line 11
Line 11:
while ($msg = $result->fetch_object())
Any help? Because I work on this thing since yesterday and I can't find the mistake.
1) Verify if your db table is named like chat_message (maybe it is chat_messages?)
2) Verify if your chat_message.id field exists
3) Verify if your php variables $id and $id2 are integer/float numbers!
$result = $db->query($sql); is returning false. This means that the query failed for some reason.
Quoting the PHP manual for mysqli::query:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or
EXPLAIN queries mysqli_query() will return a mysqli_result object. For
other successful queries mysqli_query() will return TRUE.
To check for an error you can do:
if (!$db->query($sql)) {
trigger_error('Database error: '. $db->error);
}
I get this error message, what does it mean and how to fix it?
Warning: oci_parse() expects parameter 1 to be resource, null given in /user_auth_fns.php on line 3
$conn = db_connect();
$result = oci_parse($conn, "select * from user where username='$username' and passwd = sha1('$password')");
if (!$result){
$err = oci_error();
echo "Could not log you in.";
exit;
}
$r = oci_execute($result);
if (!$r) {
$error = oci_error($conn);
echo "Could not log you in." . $error['message'];
exit;
}
function db_connect()
{
$db = "dbms";
if ($c=oci_connect("username", "password", $db)){
echo "Successfully connected to Oracle.\n";
OCILogoff($c);
} else {
$err = OCIError();
echo "Oracle Connect Error " . $err[text];
}
}
Edit 2 fixed the problem, another error message, what other compression function(other than SHA1) should use for oracle?
Warning: oci_execute() [function.oci-execute]: ORA-00904: "SHA1": invalid identifier in /user_auth_fns.php on line 10
I don't know what db_connect() is returning. Maybe it is just creating a connection by its own. Try this:
$conn = oci_connect("userName","password","hostName");
fill up the useName & password & hostName here. if you are having problem with hostName then try to put the whole connection string there. example:
$conn = oci_connect('userName', 'password', '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = )) (CONNECT_DATA = (SERVICE_NAME = ) (SID = )))');
then you can create a query like
$query="....";
then you can parse like this:
$result = oci_parse($conn, $query);
if you succeed in querying then $result holds Boolean value 'true'.
Your $conn variable is null. How do you instantiate that?
Edit
You instantiate $conn from db_connect(), which is not part of the standard PHP library so I can not tell you what's wrong with it other than it's returning null.
Edit 2
You're db_connect() doesn't return anything. Additionally, you close the connection immediately after opening it. Try this:
function db_connect()
{
$db = "dbms";
if ($c=oci_connect("username", "password", $db)){
echo "Successfully connected to Oracle.\n";
//OCILogoff($c); // You probably don't want to close the connection here
return $c;
} else {
$err = OCIError();
echo "Oracle Connect Error " . $err[text];
}
}
It means that $conn has no value, it is null. What did you want to have in $conn? Go back and check that.