Reputation: 575
I am using Dreamweaver to generate PHP and SQLi codes for connecting to a database. This I have done successfully but I keep getting the following errors:
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp\www\Maseno\Site\Template.php on line 32
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\Maseno\Site\Template.php on line 34
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\Maseno\Site\Template.php on line 34,
Here is my source code:
<?php require_once('../../Connections/connect.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = "") {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ?
mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysqli_select_db($database_connect, $connect);
$query_Users = "SELECT * FROM students";
$Users = mysqli_query($query_Users, $connect) or die(mysqli_error());
$row_Users = mysqli_fetch_assoc($Users);
$totalRows_Users = mysqli_num_rows($Users);
?>
Upvotes: 0
Views: 766
Reputation: 4752
You didn't post the part where you connect to the database, but when you connect using mysqli_connect()
, the function returns a mysqli
object (or false
on failure). If you are using procedural style, then this object must be passed as a parameter to most of your MySQLi functions.
In this code:
mysqli_select_db($database_connect, $connect); $query_Users = "SELECT * FROM students"; $Users = mysqli_query($query_Users, $connect) or die(mysqli_error()); $row_Users = mysqli_fetch_assoc($Users); $totalRows_Users = mysqli_num_rows($Users);
the calls to the following functions are incorrect:
There is also a problem with your mysqli_select_db()
call since PHP is complaining about the first argument being a string. I believe you have the parameters in the wrong order; The order of the parameters to mysqli_select_db()
is not the same as the order for mysql_select_db()
(without the i
).
Your code should look something like this:
/* $connect = mysqli_connect ( ... ); */
mysqli_select_db($connect, $database_connect);
$query_Users = "SELECT * FROM students";
$Users = mysqli_query($connect, $query_Users)
or die(mysqli_error($connect));
$row_Users = mysqli_fetch_assoc($Users);
$totalRows_Users = mysqli_num_rows($Users);
There's also a problem with this code:
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); /* ... */
You're calling mysql_real_escape_string()
(without the i
) instead of mysqli_real_escape_string()
. The point of the "real" escape function is that the function takes the character encoding of the connection into account when escaping the string. However, you can't use a MySQLi link resource with the old MySQL escape function.
Since you're using MySQLi, there should be no need to check for the existence of the "real" escape function. Your code should read simply:
$theValue = mysqli_real_escape_string($connect, $theValue);
And make sure you connect to the database first.
Note that mysqli_connect()
, mysqli_select_db()
and mysqli_query()
may return false
, and mysqli_fetch_assoc()
may return NULL
. Robust code should test (almost) all return values for error conditions.
Upvotes: 1
Reputation: 3697
You are using mysql_ and mysqli_ functions. You should only use mysqli or PDO.
mysql_ functions are already deprecated.
mysqli_ functions are a bit different then mysql_ functions. Most time you need to add an extra parameter: the connection resource.
for example:
$link = mysqli_connect("localhost", "my_user", "my_password", "my_database");
mysqli_query($link, "SHOW TABLES");
Just google "php function-name" to find information about specific functions
Upvotes: 1