create new db in mysql with php syntax - php

I am trying to create a new db called testDB2, below is my code.
Once running the script all I am getting an error on line 7
Fatal error: Call to undefined function mysqlquery() in /home/admin/domains/domain.com.au/public_html/db_createdb.php on line 7
This is my code
<?
$sql = "CREATE database testDB2";
$connection = mysql_connect("localhost", "admin_user", "pass")
or die(mysql_error());
$result = mysqlquery($sql, $connection)
or die(mysql_error());
if ($result) {
$msg = "<p>Databse has been created!</p>";
}
?>
<HTML>
<head>
<title>Create MySQL database</title>
</head>
<body>
<? echo "$msg"; ?>
</body>
</HTML>
UPDATE, now I get error
Access denied for user 'admin_user'#'localhost' to database 'testDB2'
what does this mean?

The function is called mysql_query. Note the _

Related

Save text with Tinymce online editor in Mysql database and show the saved text on the website

I have another question about connecting to Mysql database.
I have a simple text document with Tinymce online text editor. Online I want to change the text with the editor, save it to Mysql database and then show the new text online. I have the following scripts and I get this error from the file doAddContents.php:
Warning: mysql_real_escape_string(): Access denied for user ''#'localhost' (using password: NO) in /../doAddContents.php on line 8
Warning: mysql_real_escape_string(): A link to the server could not be established in /../doAddContents.php on line 8
I have no idea what I'm doing wrong. Here are the scripts:
first the script to connect to the database:
db.php:
<?php
function doDB() {
global $mysqli;
//connect to server and select database
$mysqli = mysqli_connect("localhost", "name", "pass", "db-name");
//if the connection fails, stop script execution
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
?>
doAddContents.php file:
<?php
include("db.php");
doDB();
$h4_block = "Contents Saved!";
$elm1 = $_POST['elm1'];
$entity_elm1 = htmlentities($elm1);
$entity_elm1 = mysql_real_escape_string($entity_elm1);
$add_contents_sql = "UPDATE tinymce_contents SET `contents`=
'$entity_elm1', `modified`=now()";
$add_contents_res = mysqli_query($mysqli, $add_contents_sql)
or die(mysqli_error($mysqli));
//close connection to MySQL
mysqli_close($mysqli);
//create nice message for user
$display_block = "<p>The page has been successfully updated.</p>";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div>
<h4><?php echo $h4_block; ?></h4>
<?php echo $display_block; ?>
View Page!
</div>
</body>
</html>
View.php file:
<?php
include("db.php");
doDB();
$h4_block = "View Page!";
$get_contents_sql = "SELECT * FROM tinymce_contents";
$get_contents_res = mysqli_query($mysqli, $get_contents_sql)
or die(mysqli_error($mysqli));
if ($get_contents_res = mysqli_query($mysqli, $get_contents_sql)) {
//fetch associative array
while ($row = mysqli_fetch_assoc($get_contents_res)) {
$id = $row['id'];
$contents = $row['contents'];
$modified = $row['modified'];
//Draw the results
$view_block ="<p>ID: ".$id."</p>";
$view_block .="<b>Contents</b>:".html_entity_decode($contents);
$view_block .="<b>Modified</b>:".$modified."<br/>";
}
}
//close connection to MySQL
mysqli_close($mysqli);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div>
<h4><?php echo $h4_block; ?></h4>
<?php echo $view_block; ?>
Back to Page Edit!
</div>
</body>
</html>
According to the documentation of mysql_real_escape_string. The function will try to create a mysql connection if there is no connection opened with mysql_connect().
http://php.net/manual/en/function.mysql-real-escape-string.php
You should not be using mysql_real_escape_string() but mysqli_real_escape_string()
http://php.net/manual/en/mysqli.real-escape-string.php

PHP MSQL server syntax error

I get this error when trying to get this details page for a project to work. Its for school and I dont really understand PHP that well yet.
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
Here is the code for that page.
<?php
require_once('connection.php');
mysqli_select_db($conn, $dbname);
$recordID = $_GET['recordID'];
$query_Shoe_Details = "SELECT * FROM Products WHERE Shoe_Brand = $recordID";
$Shoe_Details = mysqli_query($conn, $query_Shoe_Details) or die(mysqli_error(($conn)));
$row_Shoe_Details= mysqli_fetch_assoc($Shoe_Details);
$totalRows_Shoe_Details = mysqli_num_rows($Shoe_Details);
?>
<!DOCTYPE html>
<html>
<head>
<title>details</title><?php include 'connection.php';?>
</head>
<body>
<p>Product Name: <?php echo $row_Shoe_Details['Product_Name']; ?></p>
<p><img src=
"images/%3C?php%20echo%20$row_Shoe_Details['Image_Name'];%20?%3E"></p>
<p>Description: <?php echo $row_Shoe_Details['Product_Description']; ?></p>
<p>Price: $<?php echo $row_Shoe_Details['Product_Price']; ?></p><?php
mysqli_free_result($Shoe_Details);
?>
</body>
</html>
change your query as, use single quotes
$query_Shoe_Details = "SELECT * FROM Products WHERE Shoe_Brand = '$recordID'";
Also remove this <?php include 'connection.php';?>, No need to include again
Pl. prepare connection.php file in followng way like
$dbHost = '';
$dbUser = '';
$dbPass = '';
$dbName = '';
// setting up the web root and server root for
// this shopping cart application
$con=mysql_connect('','','');
if(!$con)
{
die('connection failed');
}
$db=mysql_select_db('',$con);
if(!$db)
{
die('db is not selected');
}
pass proper value in this syntax save it and in your code remove second line and test then and give feedback

No longer able to connect to mysql using PHP

I keep getting the following error:
Fatal error: Call to a member function fetch_array() on boolean in C:\xampp\htdocs\DeletePlayerSOLN\DeletePlayerExample_SOLN\index.php on line 10
Any idea what the problem is? I searched previous threads and tried to identify the problem for the past few hours. I'm new to PHP.
I'm using Xampp + Mysql.
MySQL connection:
127.0.0.1
3306
root
Default Schema: test
I'm connected through port 443,4433 through Xampp. So I try to connect through /localhost:443/folder..filename..
Connection:
//make a database connection object
$mysqli = new mysqli($server, $user, $pass, $database);
//test if there are database connection errors
if ($mysqli->connect_error)
die("Connect Error " . $mysqli->connect_error);
?>
Index page:
<?php
require "serverCode/connect.php";
$selectPlayer = "SELECT * FROM player ORDER BY playerLastName, playerFirstName";
$results = $mysqli->query($selectPlayer);
$ddlString = "<select name='cboPlayer' size='10'>";
while($row = $results->fetch_array())
{
$ID = $row["playerID"];
$name = $row["playerLastName"] . ", " . $row["playerFirstName"];
$ddlString .= "<option value='$ID'>$name</option>";
}
$ddlString .= "</select>";
$mysqli->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>DELETE Player Page</title>
</head>
<body>
<form name="frmPlayer" action="serverCode/deletePlayer.php" method="get">
Select a player:<p><?php echo $ddlString;?>
<input type="submit" name="btnSubmit"></p>
</form>
</body>
</html>
Line 10 is "while($row = $results->fetch_array())"
It is likely that $mysqli->query() is not producing a proper object due to an error. Try replacing
$results = $mysqli->query($selectPlayer);
with
$results = $mysqli->query($selectPlayer) or trigger_error($mysqli->error."[$selectPlayer]");

Not able to Pull from database and display in PHP

Him I'm trying to pull the max value from a row to display it in a alert message. But It gives 2 errors ..
Warning: mysql_query() [function.mysql-query]: Access denied for user 'SYSTEM'#'localhost' (using password: NO) in C:\wamp\www\test\test1.php on line 20
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\test\test1.php on line 20
please Help
<html>
<title> Test</title>
<head>
</head>
<body>
<form method="POST" action="">
<h1>Button to display data</h1>
<input type="submit" name="submit" value="PULL">
</form>
</body>
<?php
$no=20;
if(isset($_POST['submit']))
include ('airlineDB2.php');
{
$select=mysql_query("select MAX(ticketno) from ticketbook");
print '<script type="text/javascript">';
print 'alert("The no is '. $select.' is already registered")';
print '</script>';
}
?>
</html>
THIS is the connection code to the database
and i have saved it as airlineDB2.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dberror1 = "Could not connect to database";
$link = mysql_connect($dbhost, $dbuser,$dbpass) or die ($drerror1);
$selectdb = mysql_select_db('airlinedb') or die ($drerror1);
?>
Connection to database does not work. You have a typo, that's why you don't see the error:
$dberror1 = "Could not connect to database";
but later on you use
$link = mysql_connect($dbhost, $dbuser,$dbpass) or die ($drerror1);
$dberror1 != $drerror1
<?php
$no=20;
if(isset($_POST['submit']))
include ('airlineDB2.php');
{
make the include after the curly bracket

Select Mysql Data at Specific Row

I am trying to build a customer e-commerce backend. I've done stuff like this many times before and don't consider myself "new" to php & mysql, but I am stuck and can't figure out what is wrong.
I just want to display the content of a mysql row at a specific location (using the "WHERE" command).
But when I load the page, the content part (in the tables) comes up empty. There is definitely content within the table at that location and everything else on the page displays EXCEPT for the actual customerResults.
Here is my code:
<head>
<title>Customer Summary</title>
<?php
session_start();
require 'database_connect.php';
$customerTable = "customer";
if(isset($_GET['customer_click'])){
$customerId = $_GET['customer_click'];
}
?>
</head>
<h3>Customer <?php echo"$customerId"?></h3>
<table align="center" width="600px">
<tr>
<td>Summary</td>
<td>Personal</td>
<td>Billing</td>
<td>Order History</td>
</tr>
</table>
<table align="center" width="400px">
<tr>
<?php
$customerSelect = "SELECT * FROM $customerTable WHERE id = '$customerId' ";
$customerResult = mysql_query($customerSelect);
if (!$customerResult){
echo "No results, but why?!?!? </br/>";
}
if (mysql_num_rows($customerResult)==0){
echo "Results are empty...but why!?!?!";
}
while ($customerData = mysql_fetch_assoc($customerResult)){
echo $customerData['id'];
echo $customerData['email'];
}
?>
</tr>
</table>
I could be over-looking something simple, but I really can't figure this out
Let's see:
Line 27: Undefined variable 'customerSelct'.
Line 41: Undefined variable 'customerDdata'.
Line 43: Undefined variable 'result'.
Plus Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Example code using PDO:
<?php
try {
session_start();
if (!isset($_GET['customer_click'])) {
throw new Exception('Customer ID not provided.');
}
//Assuming the ID must be a number.
if (!is_numeric($_GET['customer_click'])) {
throw new Exception('Customer ID must be numeric.');
}
$customerID = $_GET['customer_click'];
$db = new PDO("mysql:host=localhost;dbname=database_name_here", "user", "pass");
//Have PDO to not emulate prepared statements by default.
//Instead use MySQL's native prepare engine.
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//PDO will throw PDOExceptions on every error.
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM `customer` WHERE `id` = :id";
$stmt = $db->prepare($query);
//Bind ID as a number and not as string.
$stmt->bindValue(":id", $customerID, PDO::PARAM_INT);
$stmt->execute();
//Fetch all results into $result.
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
//A database error has occurred!
die("Database Error occurred! " . $e->getMessage());
}
catch (Exception $e) {
//General error occurred!
die("Error! " . $e->getMessage());
}
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<pre>
<?php print_r($result); ?>
</pre>
</body>
</html>

Categories