Executing MySQLi_Query Inside a Function

Executing mysqli_query inside a function

That's a variable scope problem. You need to pass $conn to getSiteName():

function getSiteName($con) {
$row = $con->query("SELECT * FROM siteinfo")->fetch_array();
return $row["siteName"];
}

$name = getSiteName($con);

Or using a class with constructor injection:

class MyThing
{
protected $con;

public function __construct($con) {
$this->con = $con;
}

public function getSiteName() {
$row = $this->con->query("SELECT * FROM siteinfo")->fetch_array();
return $row["siteName"];
}
}

$obj = new MyThing($con);
$name = $obj->getSiteName();

Throughout the class you can use $this->con to access the connection.

How to write mysqli query in php function?

You can add in your $db using the global identifier, and then as Florian Lefèvre has said add the link to your mysqli_query().

function pagination($query, $per_page = 10,$page = 1, $url = '?')
{
global $db;

$query = "SELECT COUNT(*) as `num` FROM $query";
$row = mysqli_fetch_array(mysqli_query($db, $query));
}

running multiple queries through a single php mysqli_query function

You cannot run multiple queries through a single php mysqli_query function.

Just make your script generate an array of queries and then run them one by one in a loop.

mysqli/mysql query inside function not working

You probably need to use the global keyword, otherwise $db is considered a var in local scope.

function sanitize ($data){
global $db;
$db->mysqli_real_escape_string($data);
}

function user_exists($usermail){
global $db;
$usermail = sanitize($usermail);
$query = $db->query("SELECT COUNT(userId) FROM users WHERE userEmail= '$usermail' ");
$check = $query->num_rows;
return ($check == 1) ? true : false;
}

Can I execute function on `mysqli_error` after `or`?

As i mentioned in my comments, if you want to run your query errors in a specific function/method, then you can check either mysqli_query() return success of failure:

Example:

if (!mysqli_query($con,"Your Query")) // your query will be use here
{
myfunction(mysqli_error($con));
}

// function which you want use
function myfunction($error){ // your function will be use here
echo $error; // will print error or your function body
}

SideNote: this is just an example, you can modify this example according to your requirement.

How to use the for loop, the IN operator in same mysqli_query in PHP

You can use implode().

Usage: string implode ( string $glue , array $pieces )

mysqli_query($this->conn, "SELECT * FROM `tale_name` WHERE `id` in (". implode(',', $id_array) .")");

php mysqli query function not executing when the query contains php variable

It's hard to stare at PHP code that builds an SQL query, and try to diagnose what's wrong with the SQL query. Why make it that hard?

If you have doubts about the SQL statement, you should print out the SQL and look at the SQL, not the PHP that builds SQL. I mean the final SQL string, after you have copied PHP variables into it. It might be more clear once you look at the final query why it doesn't work.

$select = "SELECT * FROM employees 
WHERE employees.employee_fname = substring_index('$employee_coor',' ',1)
AND employees.employee_lname = substring_index('$employee_coor',' ',-1)";

error_log($select);

Try copying the final SQL and trying it in the MySQL client. Does it give zero rows? Why? Try editing the query and removing one term of the WHERE clause, then the other. Do those give zero rows?

You get the idea. Test, don't guess.

Anyway, your code has another problem: it's vulnerable to SQL injection. It will break even if you try to process a person named "O'Reilly."

I would recommend splitting the variable in PHP before you apply it to the SQL statement, and also use bound parameters.

$employee_coor = explode(' ', $_GET['employees']);

$select = "SELECT * FROM employees
WHERE employees.employee_fname = ?
AND employees.employee_lname = ?";

$stmt = mysqli_prepare($con,$select);
mysqli_stmt_bind_param($stmt, 'ss',
array_shift($employee_coor),
array_pop($employee_coor));
mysqli_stmt_execute($stmt);

Having a problem getting mysqli_query to execute

Have you checked the return value of mysqli_query()? It returns FALSE if an error occurred. In that case mysqli_error() gives you more information about the error.

<?php
$con = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$query = "INSERT INTO files VALUES (NULL, 5, 'hello')";
echo "<pre>Debug: $query</pre>\m";
$result = mysqli_query($con, $query);
if ( false===$result ) {
printf("error: %s\n", mysqli_error($con));
}
else {
echo 'done.';
}


Related Topics



Leave a reply



Submit