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 was trying to execute a SQL CREATE command with php but I have this problem:
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
I REALLY can't know where is the problem, please could you help me?
This is the script:
<?php
include 'connessione.php';
try {
$sql = 'CREATE TABLE joke (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
joketext TEXT,
jokedate DATE NOT NULL
) DEFAULT CHARACTER SET uft8 ENGINE=InnoDB';
$pdo->exec($sql);
} catch (PDOException $e){
$output = 'Errore nella creazione della tabella joke: ' . $e->getMessage();
include 'output.html.php';
exit();
}
$output = 'Tabella creata con successo.';
include 'output.html.php';
?>
And here the "connessione.php" file:
<?php
$host = 'host=localhost';
$mysql_user = 'root';
$mysql_password = '';
$database = 'php';
try {
$pdo = new PDO('mysql:$host;dbname=$database', $mysql_user, $mysql_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$output = 'Impossibile connettersi al database: ' . $e->getmessage();
include 'output.html.php';
exit();
}
?>
I checked the "connessione.php" file and the connection to database is right. Also, I checked user privileges and root has all privileges to "php" database... What's the problem?!
The problematic line is the connection itself:
$pdo = new PDO('mysql:$host;dbname=$database'
Remember that this is a string literal since they are wrapped with single quotes.
Change them you double quotes if you want the variables interpolated.
$pdo = new PDO("mysql:$host;dbname=$database", $mysql_user, $mysql_password);
It LOOKS like you're missing "host=" when you create the db handle. I'd also recommend searching SO or Google before asking questions - tons of hits for this issue: PHP PDO: Unable to connect, Invalid catalog name
Related
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
The following code is part of the main page that connect database with the main page. mainpage.php
<?php
error_reporting(E_ALL);
include_once dirname(__FILE__).'\dbh.php';
session_start()
?>
this part of the code is the connection between database with the website. dhb.php
<?php
$host = "localhost";
$user = "root";
$password = "root";
$dp="ia";
$port=8889;
$link=mysqli_init();
$conn = mysqli_real_connect($link, $host, $user , $password , $dp, $port);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql = "SELECT * FROM table player";
$result = $conn-> query($sql);
?>
This image is how the website tells me:
mysqli_real_connect() returns a boolean. It does not return a valid mysqli object. You create the mysqli object with mysqli_init().
You are overcomplicating the database connection. You only need three lines to connect properly.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
$mysqli->set_charset('utf8mb4'); // always set the charset
If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection
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 3 years ago.
Improve this question
Currently, I have one table in my database called 'factory'. In this table, there are two columns, 'Fac_ID' and 'Fac_Name'. Now, I want to create a function to add some new factory to the table 'factory'.
The value of 'Fac_ID' and 'Fac_Name' must be same, which mean when I want to add factory 'F09', the value of Fac_ID and Fac_Name must be same which is 'F09'.
When I used to connect with MYSQL database (PDO), the addition is successful. BUt when i change to MSSQL (PDO),
" Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\xampp\htdocs\ebooking\add_factory.php:24 Stack trace: #0 C:\xampp\htdocs\ebooking\add_factory.php(24): PDOStatement->bindParam(':Fac_ID', 'F11')"
Here is my code for add_factory.php
<?php
require_once "configPDO.php";
if(isset($_POST['Submit'])) {
$Fac_ID = $_POST['Fac_ID'];
// checking empty fields
if(empty($Fac_ID)) {
if(empty($Fac_ID)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$sql = "INSERT INTO factory(Fac_Name, Fac_ID) VALUES(:Fac_Name, :Fac_Name)";
$query = $conn->prepare($sql);
$query->bindParam(':Fac_Name', $Fac_ID,);
$query->bindParam(':Fac_ID', $Fac_ID,);
$query->execute();
//display success message
header("Location:factory.php");
}
}
?>
and here is my configPDO.php
<?php
$servername = 'xxx.xx.xx.xxx';
$username = 'xx';
$password = 'xxxxxx';
$dbname = 'xxxx';
try {
$conn = new PDO("sqlsrv:Server=$servername;Database=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $error) {
$error->getMessage();
}
?>
Can I know what the problem? the input at HTML to add the factory is 'Fac_ID'
in the following query
$sql = "INSERT INTO factory(Fac_Name, Fac_ID) VALUES(:Fac_Name, :Fac_Name)";
you are using :Fac_Name twice instead you should use the following
$sql = "INSERT INTO factory(Fac_Name, Fac_ID) VALUES(:Fac_Name, :Fac_ID)";
and if you need to set the same value for the name and id you should ommit the following line
$query->bindParam(':Fac_ID', $Fac_ID,);
since you are trying to bind data to a parameter that doesnt exist in your query
the following statement is sufficent in your case
$query->bindParam(':Fac_Name', $Fac_ID,);
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 5 months ago.
Improve this question
Hi i am trying to use mysqli_affected_rows function but it always return 0 can someone help. Trying to get it done theu MSQLI OOP.
<?php
$servername ="localhost";
$username ="root";
$password ="testdb";
$database ="mydb";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error)
{
die ("The Database connection is not established : " .connect_error);
}
echo "connection established";
//editing record
$sql_update = "update mytbl SET fname='Nitin Sharma' where sr=2";
echo "The affected Rows :" .mysqli_affected_rows($conn);
$conn->close();
?>
Table Values:
You missed to execute your SQL query.
$conn->query($sql_update);
You need to first execute your query by mysqli_query
$sql_update = "update mytbl SET fname='Nitin Sharma' where sr=2";
$conn->mysqli_query($sql_update);
echo "The affected Rows :" .$conn->affected_rows;
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 tried to copy How to Check if value exists in a MySQL database and make my own, but for some reason it wont work...
This is what I got:
<?php
$host = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'multiplayer';
$con=mysqli_connect($host, $username, $password, $dbname);
$check_player_ip=mysqli_query($con, 'SELECT `player_ip` FROM `playerdata` WHERE username = "remco" AND active = 0');
if (mysqli_num_rows($check_player_ip) == 0) {
//didnt find anything
} else{
//found something
}
?>
I get this error:
Parse error: syntax error, unexpected '$check_player_ip' (T_VARIABLE) in C:\xampp\htdocs\test.php on line 1
Solution
If you get the T_VARIABLE error, check the varriable before this rule. You may forgot the place the ';' xD
Thanks for all support!
You are mixing MySQL APIs, they do not mix together. mysql_num_rows
Use mysqli_num_rows()
Also make sure your DB connection is also mysqli_* and not mysql_*
EDIT after you've edited your question.
You need to pass DB connection to your query:
$check_player_ip=mysqli_query($con,'SELECT...`
$con being your DB connection. Change accordingly.
Plus WHERE username = remco - the word remco needs to be wrapped in quotes, it's a string and not an int.
WHERE username = 'remco'
Sidenote:
Your present code is open to SQL injection.
Use prepared statements, or PDO with prepared statements.
Edit 2:
Try inverting the quotes:
$check_player_ip=mysqli_query($con, "SELECT `player_ip` FROM `playerdata` WHERE username = 'remco' AND active = 0");
if (mysqli_num_rows($check_player_ip) == 0) {
//didnt find anything
}
Try this:
<?php
$host = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'multiplayer';
$con = new mysqli( $host, $username, $password, $dbname );
/* Check Connection */
if ( $con->connect_error ) {
printf( "Connect failed: %s\n", $con->connect_error );
exit();
}
/* Query - Returns a resultset */
if ( $result = $con->query('SELECT `player_ip` FROM `playerdata` WHERE username = "remco" AND active = 0 ') ) {
if ( $result->num_rows <= 0 ) {
//didnt find anything
printf("No player");
} else {
//found something
printf("Select returned %d rows.\n", $result->num_rows);
}
/* free result set */
$result->close();
}
/* close connection */
$con->close();
?>
you should execute that mysqli query...
while($rows = mysql_arrayAssoc($ursql)){
$data[]=$rows;
}
if($something== $data['attribute']) //attribute(id,name...)
echo "ok some data is in"
else
echo "no matching data"
I hope it help you :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm new to PHP and I've followed a few tutorials on PDO after learning the basics of the language and I was just wondering if my code here is correct and what you guys'd suggest I can change to make it more secure, faster, more efficient, you name it...
I've followed numerous tutorials to achieve this result and therefore I thought I'd ask you guys as not every tutorial on the web on PHP (there are so many) are reliable sources to learn best practices and writing good code.
Here's the code I have. It only inserts the string 'Bill Gates' to the database, called 'pdotest', table 'tableOne' and row 'rowOne'. I've used persistent db connection because that's supposed to make the web application faster. I'm sure you guys can enlighten me on how to use this persistent connection thing correctly, I may have not fully understood how to use this in my code.
<?php
// DB connect configuration
$user = 'user';
$pass = 'password';
// Database connection
try {
$conn = new PDO('mysql:host=localhost;dbname=pdotest', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));
}
catch(PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
$conn = null;
die();
}
// Data to insert (Bill Gates = Hero #1)
$data = 'Bill Gates';
try {
// The insert query
$sql = "INSERT INTO tableone (rowOne) VALUES (:rowOne)";
$q = $conn->prepare($sql);
$q->execute(array(':rowOne'=>$data));
// Example INSERT query with multiple VALUES
// $q->execute(array(':rowOne'=>$data, ':rowTwo'=>$dataTwo));
}
catch(PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
$conn = null;
die();
}
?>
this is apparently inefficient as your PHP have to run twice more code than needed.
the below code is enough
<?php
// DB connect configuration
$user = 'user';
$pass = 'password';
// Database connection
$dsn = "mysql:host=localhost;dbname=pdotest;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$conn = new PDO($dsn, $user, $pass, $opt);
$data = 'Bill Gates';
$sql = "INSERT INTO tableone (rowOne) VALUES (?)";
$q = $conn->prepare($sql);
$q->execute(array($data));
some highlights
use persistent connection only if you certainly know what are you doing
set exception mode if you are expecting exceptions
DO NOT catch them only to die. PHP can die all right by itself.