This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 4 years ago.
I'm attempting to connect my first php script to the mysql database. I'm using XAMPP as my server.
I keep getting the message "error querying database".
Bellow is my code. I really would appreciate the help.
//Below is how you connect to a database and insert data
//First create a variable with the connection commands so the query function you will use in a moment won't be so long
$dbc = mysqli_connect('localhost', 'root', '', 'aliendatabase') or die ('Error connecting to MySQL server.');
//Next create a variable to hold your query commands for the same reason
$query = "INSERT INTO aliens_abduction (first_name, last_name " .
"when_it_happened, how_long, how_many, alien_description, " .
"what_they_did, fang_spotted, other, email) " .
"VALUES ('Sally', 'Jones', '3 days ago', '1 day', 'four', " .
"'slimmy', 'asked questions', " .
"'yes', 'I may have seen your dog. Contact me.', " .
"'sally#gregs-list.net')";
//Now the variable that holds the query function
$result = mysqli_query($dbc, $query)
or die ('Error querying database.');
You can use
INSERT INTO table SET a=1, b=2, c=3
to avoid any confusion
Related
This question already has answers here:
php/mysql with multiple queries
(3 answers)
Closed 5 years ago.
I want to run 2 queries in one statement in a PHP page. First query converts the datetime to greek, especially the day.
$query displays the table in my page.
They work as 2 queries in my joomla plesk, mysql database with a delimiter. But not in PHP.
Query I want to implement, before the main query:
SET lc_time_names = 'el_GR'
$query = "SELECT start,DATE_FORMAT(registerdate,'%W %d %M %Y') AS registerdate FROM table1;"
$query2 = "SET lc_time_names = 'el_GR'"
I want it to run set lc time first, and then the query to display register date column. Can I do that in a single query, or 2 queries?
Regards.
You can do it in PHP too.
Take look at multiple statements in the PHP manual
Here is what your PHP code should look like:
$query = "setlocale(LC_TIME, 'el_GR.UTF-8');
SELECT start,
DATE_FORMAT(registerdate,'%W %d %M %Y') AS registerdate
FROM table1;";
$mysqli = new mysqli("example.com", "user", "password", "database");
if (!$mysqli->multi_query($query)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
do {
if ($res = $mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
i try this code to import a CSV file into my database but i got this error : Warning: mysqli::query(): Empty query
$db = new mysqli('localhost','root','', 'BD_Conference');
$sql=mysql_query("INSERT INTO tbl_conference (pid, name, chairs,keynote, abstract, speaker, affiliation, ville, pays, salle, date, time, session, image_url) VALUES ('','$champs1','$champs2','$champs3','$champs4','$champs5','$champs6','$champs7','$champs8','$champs9','$champs10','$champs11','$champs12','$champs13')");
$result = $db-> query($sql) ;
check proper mysqli connection code should be
$db= mysqli_connect('localhost','root','', 'BD_Conference');
$sql=("INSERT INTO tbl_conference (pid, name, chairs,keynote, abstract, speaker, affiliation, ville, pays, salle, date, time, session, image_url) VALUES ('','$champs1','$champs2','$champs3','$champs4','$champs5','$champs6','$champs7','$champs8','$champs9','$champs10','$champs11','$champs12','$champs13')");
$result = mysqli_query($db,$sql) ;
please use mysqli_query instead of mysql_query. So the connection is opened using mysqli.
$db = new mysqli('localhost','root','', 'BD_Conference');
if ($db->connect_errno) {
echo "Errno: " . $mysqli->connect_errno . "\n";
}
$sql="INSERT INTO tbl_conference (pid, name, chairs,keynote, abstract, speaker, affiliation, ville, pays, salle, date, time, session, image_url) VALUES ('','$champs1','$champs2','$champs3','$champs4','$champs5','$champs6','$champs7','$champs8','$champs9','$champs10','$champs11','$champs12','$champs13')";
$result = $db->query($sql) ;
This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 7 years ago.
i'm new to PHP and MySQL. I'm having issues with one of my mysqli_query functions. I have a database connection that works perfectly as the first half of my php file actually stores data into certain tables of the database.
The problem starts when i want to perform another query against the database. Here's the code:
// INSERT statements.
$queryMember = "INSERT INTO member (surname, name, gender, email, telHome, telMobile, dob, studentNum, idNum) VALUES ('$sur_name', '$f_name', '$gender', '$email', '$tel_home', '$mobile_num', ,'$date_of_birth', '$studentNumber', '$id_number')";
$queryAddress = "INSERT INTO physicalDetails (houseNum, unitNum, streetAdd, suburb, city, province, code ) VALUES ('$house_number', '$unit_number', '$street_address', '$suburb_name', '$city_name', '$province_name', '$zip_code')";
$queryStaff = "INSERT INTO staff (staffID ) VALUES ('$staff_id')";
$queryStudent = "INSERT INTO student (major ) VALUES ('$student_major')";
// Statements that must query the database.
$resultMember = mysqli_query($dbc, $queryMember) or die ('Error while inserting data into member details table');
$resultAddress = mysqli_query($dbc, $queryAddress ) or die ('Error while inserting data into member physical details table');
$resultStaff = mysqli_query($dbc, $queryStaff ) or die ('Error while inserting data into staff information table' );
$resultStudent = mysqli_query($dbc, $queryStudent ) or die ('Error while inserting data into student major details table');
mysqli_close($dbc);
When i run my form, my first error is the following: "Error while inserting data into member details table".
I don't understand why it's giving me an error. Any advice or suggestions would be appreciated. :)
Don't output a fixed (and useless) error message: Have the DB tell you what you did wrong:
$resultMember = mysqli_query($dbc, $queryMember) or die (mysqli_error($dbc));
^^^^^^^^^^^^
If you had that, you'd have been told about your syntax errors:
$queryMember = "[..snip..], '$mobile_num', ,'$date_of_birth', '$studentNumber', '$id_number')";
^^^^
I am currently trying to send data from an HTML form (Proform.html) to a MYSQL database. I cannot figure out how to solve this problem that keeps occurring as I am new to php the error message I keep receiving is.
"Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\php\Proform.php on line 22"
It could be possable as I have used several different references to create this page of code that i have mixxed two different realeases of php. Though help would be appriciated Anyways code is as follows.
<?php
$dbname='*****';
$dbhost='localhost';
$dbpass='******';
$dbuser='******';
$dbhandle = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("ecig",$dbhandle)
or die("Could not select examples");
$q = $dbhandle->query("INSERT INTO Persons (First_Name, Last_Name)
VALUES ('$_POST[First_Name]', yes)");
if (array_key_exists ('check_submit', $_POST ))
echo "Your Name is : {$_POST['First_Name']}<br />";
echo "Your Second Name is : {$_POST['Second_Name']}<br />";
echo "Your Email Address is : {$_POST['Email_Address']}<br />";
echo "Your Password Is : {$_POST['Password']}<br />";
?>
There is a problem in your query
Why are you calling the query using the database connection string.
You have to call it like this:
$con=mysql_connect($dbhost,$dbuser,$dbpass,$dbname);
$res=mysql_query("INSERT INTO Persons (First_Name, Last_Name) VALUES('$_POST[First_Name]', yes)");
mysql_close();
mysql_connect does not return any object .... so it does not have any function or property that you can access using -> operator . It returns a connection identifier
replace
$dbhandle->query("INSERT INTO Persons (First_Name, Last_Name)
VALUES ('$_POST[First_Name]', yes)");`
with
mysql_query("INSERT INTO Persons (First_Name, Last_Name)
VALUES ('$_POST[First_Name]', yes)")
I advice you to start learning mysqli or PDO since mysql functions are retarded and no longer maintained
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I am trying to insert data into a database through php.. Easy enough (I thought). I can't figure out what I am doing wrong. Here is my code:
$DB_HostName = "localhost:8888";
$DB_Name = "Sample";
$DB_User = "root";
$DB_Pass = "root";
$DB_Table = "Check";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
$sql = "INSERT INTO $DB_Table (name) VALUES ('Sally') ";
mysql_query($sql) or die ("Error with Result");
mysql_close($con);
It gives me an error saying "Error with Result". This means that it must be connecting to the database correctly and everything is working right except for the end part.. What am I missing? If I say (msql_error()) it also does tell me to check the $sql. I can't figure out though what I am typing in wrong.
escape your database name with backtick
$sql = "INSERT INTO `$DB_Table` (name) VALUES ('Sally') ";
or
$sql = "INSERT INTO `" . $DB_Table . "` (name) VALUES ('Sally') ";
CHECK is a MySQL Reserved Keyword.
MySQL Reserved Keyword List
How can I prevent SQL injection in PHP?
I can't stress this enough, don't use mysql_ functions, that time has gone. Use either mysqli or PDO.
A simple way to check what is wrong with your SQL query is to add an error flag on the end of your die statement mysql_query($sql) or die ("Error with Result<br>".mysql_error());
It appears in your case that check is a constraint used to limit the value range that can be placed in a column. You would need to identify that it is a table using "`":
$sql = "INSERT INTO `$DB_Table` (name) VALUES ('Sally') ";