mysqli_real_escape_string mysqli link? - php

I get "Notice: Undefined variable: con in C:\wamp\www\Game\functions.php on line 8" when trying to use the function, here's the code.
function protect($string) {
return mysqli_real_escape_string($con,strip_tags(addslashes($string)));
}
I use the $con for my queries and it's fine so I thought that was what was for this mysqli part?
This is for registration, I have some registration that is working but I can't use that, here's a confirmed working line
$res=mysqli_query($con,$sql);
Any ideas?

$con doesn't exist in the function protect(), so you either need to make $con global:
global $con = mysqli_connect();
or you need to pass $con as an argument:
function protect($string, $con) {
return mysqli_real_escape_string($con,strip_tags(addslashes($string)));
}

ANSWER FROM MIHAI
mysqli_real_escape_string needs a connection BEFORE it can function,use global $con; as the first line in your function. – Mihai

Related

mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in

.../general.php on line 10
Hello, this is my error, but only occurs when I upload my web to a host, in the localhost run well.
the (return mysqli_real_esc..)line is the "line 10".
$conncet = mysqli_connect('xxx','xxx','xxx','xxx');
function array_sanitize(&$datos) {
global $connect;
$datos = mysqli_real_escape_string($connect,$datos);
}
thanks for your time!!
i think you have typo in your variable, please check your $connect => $conncet variable
$conncet = mysqli_connect('xxx','xxx','xxx','xxx');
function array_sanitize(&$datos) {
global $connect;
$datos = mysqli_real_escape_string($connect,$datos);
}
You misspelt connect in your initialization. You spelt it conncet. The only reason I can think for that running well on localhost is if errors are set not to show.
first off there is a spelling error on your first line "$conncet" should be $connect
I suspect that your credentials(username & password) is wrong on the web. Find your username and password for the online database.

PHP Fatal error: Call to undefined method mysqli::mysqli_fetch_all()

hoping someone can help me, I am having the following error, looked online and tried a load of things but can't seem to figure it out, error:
Fatal error: Call to undefined method mysqli::mysqli_fetch_all() in C:\xampp\htdocs\cyberglide\core-class.php on line 38
heres my code:
<?php
class Core {
function db_connect() {
global $db_username;
global $db_password;
global $db_host;
global $db_database;
static $conn;
$conn = new mysqli($db_host, $db_username, $db_password, $db_database);
if ($conn->connect_error) {
return '<h1>'."Opps there seems to be a problem!".'</h1>'.'<p>'."Your Config file is not setup correctly.".'</p>';
}
return $conn;
}
function db_content() {
//this requires a get, update and delete sections, before its complete
$conn = $this->db_connect();
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
$query = "SELECT * FROM content";
// Escape Query
$query = $conn->real_escape_string($query);
// Execute Query
if($result = $conn->query($query)){
// Cycle through results
while($row = $conn->mysqli_fetch_all()){
//echo $row->column;
}
}
}
}
$core = new Core();
?>
I am trying to create a db_connect function, which I want to be able to call anywhere on the site that needs a database connection, I am trying to call that function on a function within the same class, I want it to grab and display the results from the database. I am running PHP 5.4.7, I am calling the database on a blank php file which includes a require to include the class file, then using this at the moment $core->db_content(); to test the function. I am building this application from scratch, running from MySQLi guides (not used MySQLi before, used to use normal MySQL query's) so if I am doing anything wrong please let me know, thanks everyone.
mysqli_fetch_all is a method of a mysqli_result, not mysqli.
So presumably it should be $result->fetch_all()
References:
http://php.net/manual/en/mysqli-result.fetch-all.php
Important: keep in mind mysqli_result::fetch_all returns the whole result set not a row as you assume in your code
There are three problems I see here.
while($row = $conn->mysqli_fetch_all()){
The method name is fetch_all() when used in the OOP way.
fetch_all() should be used with the $result object
fetch_all() is only available when the mysqlnd driver is installed - it frequently is not.
Reference
Only $result has that method. If you want to use it in a while loop use fetch_assoc(). fetch_all() returns an associative array with all the data already.
while($row = $result->fetch_assoc()){
}
thanks all, its working fine now, i had it as while($row = $conn->fetch_assoc()){
} before and changed to what i put above, but dident see it should of been $result instead of $conn, thanks for pointing that out.

switch from mysql to mysqli Undefined variable: connect

I would like to switch from mysql_* to mysqli_* because I've read, that soon mysql_* won't be supported anymore.
Now I have a problem, that the functions which I wrote dont work anymore.
At the index.php file I have
global $connect;
$connect = mysqli_connect('localhost', "username", "password");
and at the functions.php file I have
function count_total_messages(){
$result = "SELECT COUNT(id) AS total FROM database.messages WHERE uidto='".$_SESSION['userid']."' OR unameto='".$_SESSION['username']."'";
$qry = mysqli_query($connect,$result);
return $qry['total'];
}
But I get the following error:
Undefined variable: connect
What should I do? Everywhere the mysqli_query works except those from the functions.php file.
function count_total_messages(){
global $connect; //You need a locally defined connect variable
$result = "SELECT COUNT(id) AS total FROM database.messages WHERE uidto='".$_SESSION['userid']."' OR unameto='".$_SESSION['username']."'";
$qry = mysqli_query($connect,$result);
return $qry['total'];
}
You are doing the global the wrong way around. In your index you just need
$connect = mysqli_connect('localhost', "username", "password");
And in your functions you use this:
function count_total_messages(){
global $connect;
$result = "SELECT COUNT(id) AS total FROM database.messages WHERE uidto='".$_SESSION['userid']."' OR unameto='".$_SESSION['username']."'";
$qry = mysqli_query($connect,$result);
return $qry['total'];
}

php user-defined-function parameter scope assignment issue

after php code row 1 and 2, I got a notice and error message.
Notice: Undefined variable: dbc in ...
Warning: mysqli_get_host_info()
expects parameter 1 to be mysqli, null given in ...
connectdb();
echo mysqli_get_host_info($dbc);
Can you help me please to solve my notice & warning? How can I make $dbc defined also for outher functions? I am working with error level -1 by the time being. please don't tell not to display the notices as a solution. Unfortunately, I can't understand the custom function variable passage issues. thanks.
function connectdb()
{
$dbc = mysqli_connect (db_host, db_user, db_password, db_name);
if (!$dbc)
{
$txt = mysqli_connect_errno().mysqli_connect_error();
db_connect_error_mail($txt);
unset ($txt);
die('error message.');
}
else
{
return $dbc;
}
}
You aren't assigning the return value of connectdb() method anywhere. You want:
$connection = connectdb();
echo mysqli_get_host_info($connection);
For clarity I have used a different variable name as the one you use in your function, because they are different variables, as they are defined in different scopes.
Think, you are returning $dbc, from the function, but you are not assigning the return value of the connectdb() function in line 1. How will the compiler know that you saved the return value in $dbc?
$dbc = connectdb();
This will fix your error.

Problem with mysql_query() ;Says resource expected

I have this php file. The lines marked as bold are showing up the error :"mysql_query() expecets parameter 2 to be a resources. Well, the similar syntax on the line on which I have commented 'No error??' is working just fine.
function checkAnswer($answerEntered,$quesId)
{
//This functions checks whether answer to question having ques_id = $quesId is satisfied by $answerEntered or not
$sql2="SELECT keywords FROM quiz1 WHERE ques_id=$quesId";
**$result2=mysql_query($sql2,$conn);**
$keywords=explode(mysql_result($result2,0));
$matches=false;
foreach($keywords as $currentKeyword)
{
if(strcasecmp($currentKeyword,$answerEntered)==0)
{
$matches=true;
}
}
return $matches;
}
$sql="SELECT answers FROM user_info WHERE user_id = $_SESSION[user_id]";
$result=mysql_query($sql,$conn); // No error??
$answerText=mysql_result($result,0);
//Retrieve answers entered by the user
$answerText=str_replace('<','',$answerText);
$answerText=str_replace('>',',',$answerText);
$answerText=substr($answerText,0,(strlen($answerText)-1));
$answers=explode(",",$answerText);
//Get the questions that have been assigned to the user.
$sql1="SELECT questions FROM user_info WHERE user_id = $_SESSION[user_id]";
**$result1=mysql_query($sql1,$conn);**
$quesIdList=mysql_result($result1,0);
$quesIdList=substr($quesIdList,0,(strlen($quesIdList)-1));
$quesIdArray=explode(",",$quesIdList);
$reportCard="";
$i=0;
foreach($quesIdArray as $currentQuesId)
{
$answerEnteredByUser=$answers[$i];
if(checkAnswer($answerEnteredByUser,$currentQuesId))
{
$reportCard=$reportCard+"1";
}
else
{
$reportCard=$reportCard+"0";
}
$i++;
}
echo $reportCard;
?>
Here is the file connect.php. It is working just fine for other PHP documents.
<?php
$conn= mysql_connect("localhost","root","password");
mysql_select_db("quiz",$conn);
?>
$result2=mysql_query($sql2,$conn);
$conn is not defined in the scope of your function (even if you're including the connect.php file before that.
although you can use the suggestion to make $conn global, it's usually better practice to not make something global just for the sake of globalizing it.
i would instead pass $conn to the function as a parameter. this way, you can reuse the same function you wrote with different connections.
$conn isn't declared as a global so the function cannot access it, as it is not defined within it.
Either simply add
global $conn;
To the top of the function to allow it to access the $conn.
Or you can remove $conn from the mysql_query() statement. By default it will use the current connection (as mentioned in the comments below).
Where do you set $conn? It doesn't look like you set a connection within that function

Categories