im new to php and mysql (using wamp)
im getting the following error when i run my script, any idea what to fix?
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp64\www\web1\Register.php on line 79
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp64\www\web1\Register.php on line 81
line 79 t0 83
79 mysqli_select_db($database_localhost, $localhost);
80 $query_Register = "SELECT * FROM mytable";
81 $Register = mysqli_query($query_Register, $localhost) or die(mysql_error());
82 $row_Register = mysql_fetch_assoc($Register);
83 $totalRows_Register = mysql_num_rows($Register);
Here is the database connection:
<?php
# FileName="Connection_php_mysqli.htm"
# Type="mysqli"
# HTTP="true"
$hostname_localhost = "localhost";
$database_localhost = "mydatabase";
$username_localhost = "root";
$password_localhost = "";
$localhost = mysqli_connect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR);
?>
As Andrew noted in the comment, the mysql extension was deprecated in PHP 5.5.0 and removed in PHP 7.0.0. You should be using mysqli.
Nonetheless mysql expects the second parameter to be a database resource. You create a resource like this:
$dbResource = mysql_connect('localhost', 'mysql_user', 'mysql_password');
then you would select the database with something like
mysql_select_db('your_database_name', $dbResource);
i have changed the position of the connection link and database name from
mysqli_select_db($database_localhost, $localhost);
to
mysqli_select_db($localhost, $database_localhost);
the first error disappeared, but the second error still persist
forgot to mention that (html code) registration form is not showing
Related
What's wrong in the code??
<?php
$host = "localhost";
$dbuser = "tesdb";
$dbpass = "123456";
$dbname = "tesdb";
// script koneksi php postgree
$dbcon = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass);
//$query ="SELECT * FROM air_tanah.pembayaran";
$query ="select * from air_tanah.pembayaran";
$result = pg_query($dbcon, $query) or die('Query failed');
// output result
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo " Denda: " . $line['denda'] ." Penyimpan: " . $line['Penyimpan'] . "<br/>";
}
// free result
pg_free_result($result);
// close connection
pg_close($dbcon);
?>
and error like this
Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\grafig\read.php on line 12
Query failed
The code which you have entered does not allow us to really answer to your problem, we would need to know what is assigned to $dbcon variable.
pg_query expects there to be an instance of a Resource which holds the connection. Such resource is created using pg_connect or pg_pconnect method so we would need to see the contents of your db_con.php file: be sure to remove any credentials (hide them with * symbols for example).
It seems that something else actively sets the variable: are you sure that you have created a connection using pg_connect and not PDO for example (which would answer why you have there an object and not resource).
I need help with problem that appear on my website while I tried to connect to mysql.
so, this is the error:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\sites\mysite\core\connection.php on line 9
Call Stack
Time Memory Function Location
1 0.0007 163008 {main}( ) ..\index.php:0
2 0.0012 165376 include( 'C:\wamp\www\sites\mysite\core\connection.php') ..\index.php:1
3 0.0015 166400 mysql_connect ( ) ..\connection.php:9
( ! ) Warning: mysql_connect(): in C:\wamp\www\sites\mysite\core\connection.php on line 9
Call Stack
Time Memory Function Location
1 0.0007 163008 {main}( ) ..\index.php:0
2 0.0012 165376 include( 'C:\wamp\www\sites\mysite\core\connection.php') ..\index.php:1
3 0.0015 166400 mysql_connect ( ) ..\connection.php:9
MySQL Error: Accטs refusי pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
the php code to connect that I wrote, is:
<?php
session_start();
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "root";
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>
cause you are using deprecated extension so
Use mysqli or PDO (The mysql extension is deprecated)
try
mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
By default, your MySQL credentials for WAMP are:
username: root
and no password.
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "";
And as other users mentioned, try to use mysqli or pdo as mysql is deprecated, but it's not your issue here.
Good luck!
If your using the default user of phpmyadmin and didn't set a new password than the password would be an empty string (no password), unless you set your password to root.
Your code is correct, although you should look into mysqli when you feel like you can handle it.
mysql_connect — Open a connection to a MySQL Server.
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_connect()
PDO::__construct()
I have a joint table query but keep getting errors as below:
I have also recreated the database again but still getting same error. Checked my connection to mysql and works fine - is there anything wrong in my joint table query?
$select_quote=mysql_query("SELECT authors.name, authors.id, authors.img, authors.slug, quotes.author_id, quotes.title, quotes.id, quotes.meta_keys, quotes.meta_description, quotes.slug, quotes.content
from quotes, authors
WHERE quotes.author_id = authors.id
ORDER BY RAND() LIMIT 1 ");
while ($row=mysql_fetch_array($select_quote)) {
$author_id = $row['authors.id'];
$author_name = $row['authors.name'];
$author_slug = $row['authors.slug'];
echo" $author_slug";
}
This is my connection
ob_start();
error_reporting(E_ALL);
ini_set( 'display_errors','1');
$user_name = "root";
$password = "root";
$database = "quotes";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
echo "Working";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
These are the errors I am getting
Warning: mysql_query(): A link to the server could not be established
in /home/user/public_html/index.php on line 6
Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean given in /home/user/public_html/index.php on line 11
Could you please advice me on this?
Thanks
(To close this question)
You're closing your DB with mysql_close($db_handle); (you're telling it, if found then close DB connection) remove it or place it after successful query.
Plus, instead of $author_id = $row['authors.id']; do $author_id = $row['id']; and do the same for the rest.
Disclaimer: You should also consider switching to mysqli_ or PDO. mysql_ are deprecated and will be removed from future releases.
I've recently been working on a uni assignment and had a lot of trouble getting my code to work.
The errors that seem to occur when I upload my .php file onto the server and then try to view them are the following:
Warning: oci_parse() expects parameter 1 to be resource, string given in /home/contactusphp.php on line 227
Warning: ociexecute() expects parameter 1 to be resource, null given in /home/contactusphp.php on line 232
Your mesage has been sent successfully!
Additional details:
This is for use in an Oracle database, and the original purpose was for a user to use a contact form to send a message to the site owner (putting the message into the database).
My code is as follows:
211. <?
212. // extract form data
213. $emailcontact = $_REQUEST['emailcontact'] ;
214. $email_address = $_REQUEST['email_address'] ;
215.
216. // Create the SQL statement to add data into the database
217. $sql = "INSERT INTO contactus (emailcontact, email_address) VALUES ('$emailcontact', '$email_address')";
218.
219. // Set the oracle user login and password info
220. $dbuser = 'XXXX';
221. $dbpass = 'XXXX';
222. $db = 'SSID';
223. $connect = 'OCI_Logon($dbuser, $dbpass, $db)';
224.
225.
226. // Add this data into the database as a new record
227. $stmt = OCI_Parse($connect, $sql);
228. if(!stmt) {
229. echo 'An error occurred in parsing the SQL string./n';
230. exit;
231. }
232. OCI_Execute($stmt); {
233. echo ('Your mesage has been sent successfully!');
234. }
235. ?>
I can't seem to find what could be wrong, and I'm not very experienced with web development either.
EDIT: I got rid of quotes, and changed OCI_Logon/OCI_Parse/OCI_Execute to OCILogon, etc.
However, the problem changed when I did so.
There's a new error code, which is as follows:
Warning: ociexecute() [function.ociexecute]: ORA-00904: "EMAILCONTACT": invalid identifier in /home/contactusphp.php on line 232
The new code is:
211. <?
212. // extract form data
213. $emailcontact = $_REQUEST['emailcontact'] ;
214. $email_address = $_REQUEST['email_address'] ;
215.
216. // Create the SQL statement to add data into the database
217. $sql = "INSERT INTO contactus (emailcontact, email_address) VALUES ('$emailcontact', '$email_address')";
218.
219. // Set the oracle user login and password info
220. $dbuser = 'XXXX';
221. $dbpass = 'XXXX';
222. $db = 'SSID';
223. $connect = OCILogon($dbuser, $dbpass, $db);
224.
225.
226. // Add this data into the database as a new record
227. $stmt = OCIParse($connect, $sql);
228. if(!stmt) {
229. echo 'An error occurred in parsing the SQL string./n';
230. exit;
231. }
232. OCIExecute($stmt); {
233. echo ('Your mesage has been sent successfully!');
234. }
235. ?>
EDIT:
The problem ended up fixing itself, and I have no idea how.
Why the quotes around this function?
$connect = 'OCI_Logon($dbuser, $dbpass, $db)';
I am trying to add a section in my site where you can fill out a small form and it will add the neighborhood in for you. When I try to fill in the form and add the new neighborhood, I get the error below. Please let me know what I am doing wrong and why I can't get these to be added into the database? Thanks for any help!
Code:
require_once('db.php');
//set the registration variables
$name = addslashes($_POST['name']);
$builder = addslashes($_POST['builder']);
$builderURL = $_POST['builderURL'];
//connect to database
$link = mysql_connect($dbhost, $dbuser, $dbpwd);
if (!$link) {
die(strip_tags('Could not connect: '.mysql_error()));
}
//add this contest to the database
$stmt = "INSERT INTO hf_neighborhoods (name, builder, builder_url, created_on) VALUES " .
"('$name', '$builder', '$builderURL', NOW())";
$result = mysql_query($dbname, $stmt);
if(!$result){
die(strip_tags('Error Adding Neighborhood: '.mysql_error()));
}
//if we made it this far, return a success
echo 'success';
?>
DB Call:
<?php
$dbhost = 'localhost';
$dbname = 'delsur_2011_dev';
$dbuser = 'delsur2011';
$dbpwd = 'newsite!';
?>
Error:
<br />
<b>Warning</b>: mysql_query() expects parameter 2 to be resource, string given in <b>/var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php</b> on line <b>21</b><br />
Error Adding Neighborhood:
Now I am receiving this error when I try to change it to that new code? Is it just not connecting to the database at all now? I am lost, I wasn't the one that set this file up, I was just told to go and try to fix it so that is what I am trying to do now? Thanks for your help
Warning: mysql_real_escape_string(): Access denied for user 'apache'#'localhost' (using password: NO) in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 8
Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 8
Warning: mysql_real_escape_string(): Access denied for user 'apache'#'localhost' (using password: NO) in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 9
Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 9
Error Adding Neighborhood: No database selected
You have to change
$result = mysql_query($dbname, $stmt);
with
$result = mysql_query($stmt, $link);
However, using mysql_* functions is deprecated and its now considered a bad practice.
Instead you should be using PDO or MySQLi!
And, dont use addslashes, at least use mysql_real_escape_string
EDIT: You should be calling mysql_real_escape_String() after you connect to the database, and not before. And also, dont forget to call mysql_select_db()!
//connect to database
$link = mysql_connect($dbhost, $dbuser, $dbpwd);
if (!$link) {
die(strip_tags('Could not connect: '.mysql_error()));
}
mysql_select_db($dbname, $link);
// Sanitize your input at least!
$name = mysql_real_escape_string($_POST['name'], $link);
$builder = mysql_real_escape_string($_POST['builder'], $link);
$builderUrl = mysql_real_escape_string($_POST['builderUrl'], $link);
//add this contest to the database
$stmt = "INSERT INTO hf_neighborhoods (name, builder, builder_url, created_on) VALUES " .
"('$name', '$builder', '$builderURL', NOW())";
$result = mysql_query($stmt, $link);
...
...
...