Returning row array [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a database which I'm querying: "select email from users where access=1". The error I'm receiving is
Fatal error: Call to a member function query() on a non-object on line: $result = $mysqli->query($query);
I'm using http://uk3.php.net/manual/en/mysqli.query.php as a reference and I can't see where I'm querying a mixture of Procedural and Object oriented PHP.
<?php
$db_host = "";
$db_user = "";
$db_pass = "";
$db_name = "";
/* OOP MYSQLI DATABASE CONNECTION */
$db = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
/*LOGIN QUERY */
$query = "select email from users where access=1";
$result = $mysqli->query($query);
$email= $result->fetch_array(MYSQLI_NUM);
if(isset($email[0])){
$query1="select id from user where email='".$email[0]."' and facebook=1";
$result1 = $mysqli->query($query1);
$email1= $result1->fetch_array(MYSQLI_NUM);
$userId=$admin[0];
}
?>
Would someone mind explaining please?

Your MySQLi object is not named $mysqli, its named $db
$result = $mysqli->query($query);
Should be
$result = $db->query($query);
Same goes for
if ($mysqli->connect_error) {
Should be
if ($db->connect_error) {
Edit
Just a side note, your second query makes no sense at all :) You just fetched an email from that table and then you are querying the same table again for that email?
$email= $result->fetch_array(MYSQLI_NUM);
if(isset($email[0])){
$query1="select id from user where email='".$email[0]."' and facebook=1";
All that setup is equivalent to your first query being
select id,email from users where access=1 and facebook=1
And you don't need that second query then

Related

Fetching data from a temporary table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am not able to fetch data from a temporary table that I have just created with a SELECT statement off another table.
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();}
$query = "create temporary table temp1 select name from trn_games";
$query2 = "select name from temp1";
$result = mysqli_query($link, $query2) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo $row['name'];
}
Note: To create a table by SQL Query you should execute it by mysqli_query($link, $query);.
You only write SQL Query but do not execute it.
$query = "create temporary table temp1 select name from trn_games";
$execute = mysqli_query($link, $query); // here execute your SQL QUERY.
$query2 = "select name from temp1";
$result = mysqli_query($link, $query2);
while($row = mysqli_fetch_array($result)) {
echo $row['name'];
}

How to PHP MySQL query a STRING? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a string called $searchquery. $searchquery contains this SQL command:
SELECT * FROM `videos` WHERE (`Title` LIKE "%query%" OR `Tags` LIKE "%query%")
How can I make MySQL execute this command from a PHP page then display the results on PHP page??
I have tried
$sql = ($searchquery)
But dosent seem to work?
Firstly you need to create database connection
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// then execute your query
$sql = "SELECT * FROM `videos` WHERE (`Title` LIKE '%query%' OR `Tags` LIKE '%query%')";
$result = mysqli_query($conn, $sql);
// and fetch result set
while($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
?>

simple mysqli query dont work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I try to learn mysql an have a problem with a sql query.
i made a database connection with mysqli
the php script create a table code_scanned in database.
Creating the Table works very good!
But my second query to fill the table wont work :(
If i paste the query manually to phpadmin it works.
But not in my php script.
Can please anyone have a look on my code, what i do wrong.
$servername = "xxxxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE IF NOT EXISTS code_scanned (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
produkt VARCHAR(50) NOT NULL,
code VARCHAR(30) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "code_scanned erstellt";
} else {
echo "Fehler" . $conn->error;
}
$sql = "INSERT INTO code_scanned (produkt, code) VALUES ('gates', 'Microsoft')";
$conn->close();
Exexute the insert query :
And correct The code:
$sql = "INSERT INTO code_scanned (produkt, code) VALUES ('gates', 'Microsoft')";
$result= mysqli_query($conn,$sql);
if($result)
{
echo "Insert sucessfully";
}
else
{
echo("Sorry:".mysqli_errno($conn));
}
$conn->close();
You are not running the second query. Put this below the line that has $sql = "INSERT...."
$conn->query($sql);

Printing last 10 entries in database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I'm working on a website that has a list of novels in a database with some basic info about them. I'd like to make a table of the most recent additions to the database. I'm using PHP and SQL and this is what I've got so far.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "novels";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select ten most recent entries
SELECT `N_ID`, `NAME`, `DATE_RELEASED`, `GENRES` FROM basic_info ORDER BY N_ID DESC LIMIT 10
?>
I"m new to PHP And SQL so what I can gather is that I've made a connection to the database and have pulled the information from the latest 10 entries. Now I'm just not sure how to print them.
Any help is appreciated!
Try to use PDO if you can. Also you could use lower case for your columns to avoid case sensitivity issues.
You have to "wrap" your SELECT query in a variable (e.g. $sql) to be able to pass it in your php code.
error_reporting(E_ALL);
ini_set("display_errors", 1);
$servername = "localhost";
$username = "root";
$password = "password";
$database = "novels";
try {
//Make your connection handler to your database
$conn = new PDO("mysql:host=".$servername.";dbname=".$database, $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
$sql = "SELECT `N_ID`, `NAME`, `DATE_RELEASED`, `GENRES` FROM basic_info ORDER BY N_ID DESC LIMIT 10";
$stmt = $conn->prepare($sql);
//Execute the query
$stmt->execute();
$result = $stmt->fetchAll();
//Fetch the results
foreach ($result as $row) {
echo '<p>'.$row['NAME'].'</p>';
}
} catch(PDOException $e) {
echo $e->getMessage();
die();
}

error in your SQL syntax; [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
hi i am getting this error though i tried to change the new to i am stil getting this problem can anyone tell me what should i do. I have completely changed the page also database but still same error.
error>
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''calendar_admin' WHERE teacher_id='ipcst123' and password='a141c47927929bc2d1fb6'
at line 1
my code >
<?php
$username=$_POST['teacherId'];
$password=$_POST['password'];
$password=md5($password);
try {
$bdd = new PDO('mysql:host=localhost;dbname=XXX', 'XXX', 'XXX');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
$query ="SELECT * FROM 'calendar_admin' WHERE teacher_id="."'".$username."' and password="."'".$password."' ";
$resultat = $bdd->query($query) or die(print_r($bdd->errorInfo()));
$res = $resultat->fetchAll(PDO::FETCH_ASSOC);
foreach($res as $result){
$pass=md5($password);
$user=$result["teacher_id"];
if ($pass==$result["password"]&& $username == $user ){
echo "login Success";
session_start();
$_SESSION['teacher_id'] = $username;
header('Location:/addEvents.php');
}else{
header('Location:/login.php');
//echo "Incorrect Password";
}
}
You should use backticks instead of single quotes :
$query ="SELECT * FROM `calendar_admin` WHERE teacher_id='".$username."' and `password`='".$password."' ";
or just remove them
$query ="SELECT * FROM calendar_admin WHERE teacher_id='".$username."' and `password`='".$password."' ";
And since you use PDO, you should bind parameters, but not concatenate them into the query:
$query ="SELECT * FROM calendar_admin WHERE teacher_id= :teacher and `password`= :password ";
$sth = $bdd->prepare($query);
$sth->bindParam(':teacher',$username);
$sth->bindParam(':password',$password);
$sth->execute();
$res = $sth->fetchAll(PDO::FETCH_ASSOC);
Around column and table names has to be backticks, not single quotes. O rnothing if the names aren't from reserved words (or spaces, or hyphens, or anything else that MySQL will scream about, #Fred -ii- in comments below):
`calendar_admin`
The full query:
$query ="SELECT *
FROM `calendar_admin`
WHERE teacher_id = '" . $username . "' AND
password = '" . $password . "'";
Don't forget to escape data from user inputs.
$query = "
SELECT *
FROM calendar_admin
WHERE teacher_id = '$username'
AND password = '$password';
";
Next, take a look at prepared statements

Categories