I have re-written this question because of all the down votes and it would seem that either no one understood the question or was unwilling to help a newbie. So I'll as it this way: Can someone tell/show me why this code does not work? (500 error)
OR... at the very least point me in the right direction? I AM WILLING TO LEARN, I just don't know where to begin or who/where to learn from as I am not sure what to even ask other than how do you run multiple "else" statements... that however left me more confused than I already am though!
Any "HELP" would be greatly appreciated!
<?php
$servername = "localhost";
$username = "****";
$password = "***";
$dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$bname = $_POST['bname'];
$baddress = $_POST['baddress'];
$bcity = $_POST['bcity'];
$bstate = $_POST['bstate'];
$zipcode = $_POST['zipcode'];
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Login FROM `USERS` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
echo "https://www.***.com/realsite.php?Login=" . $row["Login"] . "";
}
}
else
{
$sql = "SELECT Login FROM `DATA` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
echo "https://www.***.com/demo.php?Login=" . $row["Login"] . "";
}
}
else
{
$sql = "INSERT INTO `DATA` (Business_Name, Business_Address, Business_City, Business_State, Business_Zip)
SELECT '$bname', '$baddress', '$bcity', '$bstate', '$zipcode' FROM (SELECT 1) t
WHERE NOT EXISTS (SELECT Login, Business_Name,Business_Address FROM `DATA` WHERE Business_Name='$bname' AND Business_Address='$baddress')";
if (mysqli_query($conn, $sql))
{
echo "<a href='https://www.servedwell.com/realsite.php?Login=" . $row["Login"] . "'>LINK</a>";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
}
?>
Credit goes to #Ancaron for properly formatting code and correcting a syntax error. Final posted code works fine.
While selecting rows the data should be shown as:
but on the running code it is as:
My Codes:
<?php
$link = mysqli_connect("localhost", "root", "", "trial");
if(!$link){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$term = 'd';//mysqli_real_escape_string($link, $_REQUEST['term']);
if(isset($term)){
$sql = "SELECT * FROM rolls WHERE Place LIKE '%" . $term . "%'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
if(mysqli_num_rows($result) > 1000){
echo "<p>There are many results please be more specific</p>";
}
else{
while($row = mysqli_fetch_array($result)){
echo "<p><a href='#". $row['Place'] ."'>" . $row['Place'] . "</a></p>";
}
mysqli_free_result($result);
}
} else{
echo "<p>No matches found</p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_close($link);
?>
On trying
$sql = "SELECT * FROM rolls WHERE Place LIKE '%" . $term . "%' LIMIT 1";
it only displays MANDYA, rest all are skipped
If you just want to display the place, use DISTINCT for showing unique values. Check out this query.
$sql = "SELECT DISTINCT(Place) FROM rolls WHERE Place LIKE '%" . $term . "%'";
I am trying to remove a user from the mailing list right before I perform the emailing to my users. However, the code responsible for this within the second while loop is not executing.
Can somebody see why this is?
<?php
$query = "SELECT * FROM remove_request";
$result = mysqli_query($dbc,$query);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$query = "DELETE FROM mail_table WHERE first_name='" . $row["first_name"]
. "' AND last_name='" . $row["last_name"]
. "' AND email_address='" . $row["email_address"] . "'";
$result = mysqli_query($dbc,$query);
echo $row["first_name"] . ' has been removed from the mail list.';
}
}
$query = "SELECT * FROM mail_table";
$result = mysqli_query($dbc,$query) or die('Error querying database');
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
mail($row["email_address"],$subject,$message,$headers);
echo 'email sent to ' . $row["first_name"] . "\n";
}
}
mysqli_close($dbc);
?>
Trying to pull data out of a basic phpmyadmin database.
The code below pulls the data correctly (Commented out section verify).
I can write it to the screen and display it. (Not needed just testing)
Trying to insert it into another database however and it fails.
I've discovered that the while loops for inserting do not run. Although I can not find out why.
It's a basic localhost database (Testing right now) So the connect data is just temporary.
Any assistance is greatly appreciated
Thanks.
<?php
/*
Connect to database
*/
$webhost = 'localhost';
$webusername = 'root';
$webpassword = '';
$webdbname = 'transfertest';
$webcon = mysqli_connect($webhost, $webusername, $webpassword, $webdbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/*
*
*/
$questions = mysqli_query($webcon, "SELECT * FROM questions");
$scenarios = mysqli_query($webcon, "SELECT * FROM scenarios");
$results = mysqli_query($webcon, "SELECT * FROM results");
$employees = mysqli_query($webcon, "SELECT * FROM employees");
/*
* These while loops display the content being pulled from the database correctly.
while ($row = mysqli_fetch_array($questions)) {
echo $row['questionID'] . " : " . $row['question'] . " : " . $row['answers'];
echo "</br>";
}
while ($row = mysqli_fetch_array($scenarios)) {
echo $row['scenarioID'] . " : " . $row['scenarioTitle'] . " : " . $row['scenarioInformation'];
echo "</br>";
}
while ($row = mysqli_fetch_array($results)) {
echo $row['employeeID'] . " : " . $row['scenarioID'] . " : " . $row['questionID'] . " : " . $row['answers'] . " : " . $row['correct'];
echo "</br>";
}
while ($row = mysqli_fetch_array($employees)) {
echo $row['employeeID'] . " : " . $row['firstName'] . " : " . $row['lastName'] . " : " . $row['email'] . " : " . $row['password'];
echo "</br>";
}
*/
/* //////////////////////////////////////////////////////////////////////////
Connect to database
*/
$mobhost = 'localhost';
$mobusername = 'root';
$mobpassword = '';
$mobdbname = 'exampletransfer';
$mobcon = mysqli_connect($mobhost, $mobusername, $mobpassword, $mobdbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/*
*
*/
while ($row = mysqli_fetch_array($questions)) {
mysqli_query($mobcon, "INSERT INTO questions (questionID, question, answers) VALUES (" . $row['questionID'] . ", " . $row['question'] . ", " . $row['answers'] . ")");
}
while ($row = mysqli_fetch_array($scenarios)) {
mysqli_query($mobcon, "INSERT INTO scenarios (scenarioID, scenarioTitle, scenarioInformation) VALUES (" . $row['scenariosID'] . ", " . $row['scenarioTitle'] . ", " . $row['scenarioInformation'] . ")");
}
while ($row = mysqli_fetch_array($results)) {
mysqli_query($mobcon, "INSERT INTO results (employeeID, scenarioID, questionID, answers, correct) VALUES (" . $row['employeesID'] . ", " . $row['scenariosID'] . ", " . $row['questionID'] . ", " . $row['answers'] . ", " . $row['correct'] . ")");
}
while ($row = mysqli_fetch_array($employees)) {
mysqli_query($mobcon, "INSERT INTO employees (employeeID, firstName, lastName, email, password) VALUES (" . $row['employeesID'] . ", " . $row['firstName'] . ", " . $row['lastName'] . ", " . $row['email'] . ", " . $row['password'] . ")");
}
/*
Close Connections
*/
mysqli_close($webcon);
mysqli_close($mobcon);
/*
* Error code:
Notice: Undefined index: scenariosID on line 75
Notice: Undefined index: employeesID on line 78
Notice: Undefined index: scenariosID on line 78
Notice: Undefined index: employeesID on line 81
*/
?>
The problem is that you close your $webcon connection and then you try to read from it ^^
You try to do this... Thats not possible ;)
Prepare query mysqli_query($webcon, "SELECT * FROM questions");
Close connection <<< after that i cant read data
Read data
Try this please.
<?php
/**
* Connect to database
*/
$webhost = 'localhost';
$webusername = 'root';
$webpassword = '';
$webdbname = 'transfertest';
$webcon = mysqli_connect($webhost, $webusername, $webpassword, $webdbname);
if (mysqli_connect_errno())
{
echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
/**
* Queries for reading
*/
$questions = mysqli_query($webcon, 'SELECT * FROM `questions`');
$scenarios = mysqli_query($webcon, 'SELECT * FROM `scenarios`');
$results = mysqli_query($webcon, 'SELECT * FROM `results`');
$employees = mysqli_query($webcon, 'SELECT * FROM `employees`');
/**
* Connect to database
*/
$mobhost = 'localhost';
$mobusername = 'root';
$mobpassword = '';
$mobdbname = 'exampletransfer';
$mobcon = mysqli_connect($mobhost, $mobusername, $mobpassword, $mobdbname);
if (mysqli_connect_errno())
{
echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
/**
* Insert data from old database
*/
// questions
while ($row = mysqli_fetch_array($questions))
{
// escape your strings
foreach($row as $key => $val)
{
$row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
}
mysqli_query($mobcon, "INSERT INTO `questions` (`questionID`, `question`, `answers`) VALUES ('" . $row['questionID'] . "', '" . $row['question'] . "', '" . $row['answers'] . "');");
}
// scenarios
while ($row = mysqli_fetch_array($scenarios))
{
// escape your strings
foreach($row as $key => $val)
{
$row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
}
mysqli_query($mobcon, "INSERT INTO `scenarios` (`scenarioID`, `scenarioTitle`, `scenarioInformation`) VALUES ('" . $row['scenariosID'] . "', '" . $row['scenarioTitle'] . "', '" . $row['scenarioInformation'] . "');");
}
// results
while ($row = mysqli_fetch_array($results))
{
// escape your strings
foreach($row as $key => $val)
{
$row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
}
mysqli_query($mobcon, "INSERT INTO `results` (`employeeID`, `scenarioID`, `questionID`, `answers`, `correct`) VALUES ('" . $row['employeesID'] . "', '" . $row['scenariosID'] . "', '" . $row['questionID'] . "', '" . $row['answers'] . "', '" . $row['correct'] . "');");
}
// employees
while ($row = mysqli_fetch_array($employees))
{
// escape your strings
foreach($row as $key => $val)
{
$row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
}
mysqli_query($mobcon, "INSERT INTO `employees` (`employeeID`, `firstName`, `lastName`, `email`, `password`) VALUES ('" . $row['employeesID'] . "', '" . $row['firstName'] . "', '" . $row['lastName'] . "', '" . $row['email'] . "', '" . $row['password'] . "');");
}
/*
Close Connections
*/
mysqli_close($mobcon);
mysqli_close($webcon);
Pending it's on the same server and using the same username and password:
// Create a new MySQL database connection
if (!$con = mysql_connect('localhost', $username, $password)) {
die('An error occurred while connecting to the MySQL server!<br/>' . mysql_error());
}
if (!mysql_select_db($database)) {
die('An error occurred while connecting to the database!<br/>' . mysql_error());
}
// Create an array of MySQL queries to run
$sql = array(
'DROP TABLE IF EXISTS `exampletransfer.questions`;',
'CREATE TABLE `exampletransfer.questions` SELECT * FROM `transfertest.questions`'
);
// Run the MySQL queries
if (sizeof($sql) > 0) {
foreach ($sql as $query) {
if (!mysql_query($query)) {
die('A MySQL error has occurred!<br/>' . mysql_error());
}
}
}
If using MySQLi instead of MySQL:
// Create a new MySQL database connection
if (!$con = new mysqli('localhost', $username, $password, $database)) {
die('An error occurred while connecting to the MySQL server!<br/>' . $con->connect_error);
}
// Create an array of MySQL queries to run
$sql = array(
'DROP TABLE IF EXISTS `exampletransfer.questions`;',
'CREATE TABLE `exampletransfer.questions` SELECT * FROM `transfertest.questions`'
);
// Run the MySQL queries
if (sizeof($sql) > 0) {
foreach ($sql as $query) {
if (!$con->query($query)) {
die('A MySQL error has occurred!<br/>' . $con->error);
}
}
}
$con->close();
I made a function that loops through all the posts in database and echo them. But there's a slight problem. I am getting whitespace between the name (title of the post) and the content, its like two lines or so. I want reason for this and solution. Thanks.
function read_all_posts(){
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'website';
$con = mysqli_connect($host, $username, $password, $database);
$query = "SELECT * FROM posts";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo "<h1>" . $row['name'] . "</h1>" . "<br>" . $row['content'] . "<br />" . "<i>" . $row['author'] . "</i>";
echo "<br>";
}
}
try,
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo "<h1>" . $row['name'] . "</h1>" . "<p>" . $row['content'] . "</p>" . "<p><i>" . $row['author'] . "</i></p>";
}