PHP No database found - php

So it says no database selected, please help me as my hosting cant
<?php
$sitename = "http://devilgaming.phy.sx/csgopie.xyz/";
$link = #mysql_connect("localhost", "devilgam_csgopie", "ssb12boycek1");
$db_selected = mysql_select_db('devilgam_csgopie', $link);
mysql_query("SET NAMES utf8");
function fetchinfo($rowname,$tablename,$finder,$findervalue)
{
if($finder == "1")
$result = mysql_query("SELECT $rowname FROM $tablename");
else
$result = mysql_query("SELECT $rowname FROM $tablename WHERE
`$finder`='$findervalue'") or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>

Where is says
$result = mysql_query("SELECT $rowname FROM $tablename WHERE
`$finder`='$findervalue'") or die (mysql_error());
The mysql_fetch_assoc() function returns an associative array, row by row. To access the rows you would do:
while ($row = mysql_fetch_assoc($result)) {
echo $row[$rowname];
}

According situations when You install purchased script, or You are comfort with mysql_* functions, I'll not say to switch to mysqli or PDO. PHP 7 will force to switch to modern drivers to everyone (:
but for Your question, run this script and tell me result:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$sitename = "http://devilgaming.phy.sx/csgopie.xyz/";
$link = mysql_connect("localhost", "devilgam_csgopie", "ssb12boycek1") or die(mysql_error());
mysql_select_db('devilgam_csgopie', $link) or die(mysql_error());
mysql_query("SET NAMES utf8");
function fetchinfo($rowname, $tablename, $finder, $findervalue) {
$q = "SELECT $rowname FROM $tablename";
if($finder != 1) {
$q .= " WHERE `$finder`='$findervalue'";
}
$result = mysql_query($q) or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>
Also go to administration panel of hosting create or modify user: devilgam_csgopie to have same username and password.
Because:
Access denied for user 'devilgam_csgopie'#'localhost' to database

Related

Table 'databasename.info' doesn't exist

So I installed this jackpot script with a layout and everything and within the jackpot script there was a set.php file which I tried to set up, it looked like this:
<?php
$sitename = "csgoxd.net";
$link = #mysql_connect("localhost:3306", "csgoxdne", "thisisasecretpassword");
$db_selected = mysql_select_db('csgoxdne_csgoxddb', $link);
mysql_query("SET NAMES utf8");
function fetchinfo($rowname,$tablename,$finder,$findervalue) {
if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");
else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'") or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>
So I'm new when it comes to coding in general (I know some basic stuff but that's it) so basically I'm not sure if I'm supposed to fill out more of this file because I get this error on my website.
"Table 'csgoxdne_csgoxddb.info' doesn't exist"
I'm new to this and I'm trying to learn so help is much appreciated.
You should use MySQLi to make use of its advantages it offers over MySQL. You can see more here.
The script you have isn't all too bad, but it does need some tweaking. It's vulnerable to injection like Marc B said. I'm going to assume that csgoxdne_csgoxddb is your table name.
Try this:
<?php
$mysqli = new mysqli("localhost:3306", "csgoxdne", "thisisasecretpassword");
if (mysqli -> error){ print ("Error connecting! Message: ".$mysqli->error); }
mysqli_set_charset($mysqli, 'utf8');
function fetchinfo($rowname, $tablename, $finder, $findervalue) {
if ($finder == "1") {
$query = "SELECT * FROM $tablename WHERE rowname = '$rowname'";
$result = mysqli_query($mysqli, $query);
} else {
$query = "SELECT * FROM $tablename WHERE `$finder`='$findervalue'";
if (!$query) {
die('Invalid query: ' . $mysqli->error);
}
$result = mysqli_query($mysqli, $query);
}
return $result;
}
?>
Oh and make sure the port number on your localhost is correct.
Also to go through the values of result you can use:
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
#do things
}
}

Getting Data From Multiple MySQL Tables Using PHP and mysqli

I am trying to draw data from multiple tables that have been indexed to relate to one another. I ran this query in MySQLWorkbench, and it ran successfully. However when I tried to run a PHP test, nothing showed up, not even for the first field. Here is my code:
<?php
$db = new mysqli('host', 'user', 'password', 'database');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "
SELECT
`Contact`.`firstName`,
`Contact`.`lastName`,
`ssn`.`ssn`,
`Contact`.`country`,
`Allergies`.`allergy`,
`Allergies`.`allergyType`,
`Allergies_Contact`.`allergyNotes`,
`CurrentPrescriptions`.`prescriptionName`,
`CurrentPrescriptions`.`prescribedDate`,
`BloodType`.`bloodType`
FROM
`database`.`Contact`,
`database`.`Allergies_Contact`,
`database`.`Allergies`,
`database`.`ssn`,
`database`.`CurrentPrescriptions`,
`database`.`BloodType`
WHERE
`Contact`.`contactKey` = `Allergies_Contact`.`contactKey`
AND `Allergies`.`allergiesKey` = `Allergies_Contact`.`allergiesKey`
AND `ssn`.`contactKey` = `Contact`.`contactKey`
AND `CurrentPrescriptions`.`contactKey` = `Contact`.`contactKey`
AND `BloodType`.`contactKey` = `Contact`.`contactKey`;
";
$result = $db->query($query) or die($db->error.__LINE__);
if ($result = mysqli_query($db, $query)) {
while ($row = mysqli_fetch_row($result)) {
print(row[0]);
}
mysqli_free_result($result);
}
mysqli_close($db);
?>
Please tell me what I am doing wrong here, because from what I can see its formatted correctly.
Several things:
1.- You have two query sentences, change:
$result = $db->query($query) or die($db->error.__LINE__);
if ($result = mysqli_query($db, $query)) {
With this
$result = $db->query($query) or die($db->error.__LINE__);
if ($result !== false) {
2.- Yo made a mistake when trying to print the variable, change:
while ($row = mysqli_fetch_row($result)) {
print(row[0]);
}
With this
while ($row = mysqli_fetch_row($result)) {
print($row[0]); // You missed a $
}
<?php
//conection:
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
//consultation:
$query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = $link->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["name"] . "<br>";
}
?>
http://php.net/manual/en/function.mysqli-connect.php

Connect php with server

I am connecting to server with help of php for an android application.
Name of Database in phpmyadmin is "student" , name of table is "data" and fields are "Name" and "EmpId"
This is what I coded n php and getting the error on the "$output" part as undefined variable
Here is the code:
<?php
$connection = connectionserver ();
function connectionserver (){
$con = mysql_connect("localhost", "root", "") or die ("connection not found");
if($con)
echo "Connection Created" ,"<br>";
$database = mysql_select_db ("student1", $con);
if($database) echo "Database Connected" , "<br>";
return $con;
}
$result = mysql_query("select * from data");
while ($row = mysql_fetch_assoc($result))
{
$output [] = $row;
}
print json_encode($output);
mysql_close($connection);
?>
declare $output as array before the while
$output = array();
$undefined_array[] = 'something' will not trigger an E_NOTICE error. However it is good practice to initialize the variable.
The error comes from the line with json_encode, most likely because your query didn't return any result, didnt get into the while loop, thus $output[] was never executed.
You may try this:
<?php
$connection = connectionserver ();
function connectionserver (){
$con = #mysql_connect("localhost","root","");
if(!$con) die("Can't connect!!");
$var2 = #mysql_select_db("student1",$con);
if(!$var2)
die("<br>"."can't select dataBase");
$result = mysql_query("select * from data");
while ($row = mysql_fetch_assoc($result))
{
$output[] = $row;
}
print json_encode($output);
mysql_close($con);
}
?>
Try this,
echo connectionserver();
function connectionserver (){
$con = mysql_connect("localhost", "root", "") or die ("connection not found");
$database = mysql_select_db ("student1", $con);
$result = mysql_query("select * from data") or die(mysql_error());
$output = array();
while ($row = mysql_fetch_assoc($result))
{
$output[] = $row;
}
mysql_close($con);
return json_encode($output);
}

Store mysql column to array PHP

I use this to get a column named "device_token" and save the values to an array:
mysql_connect("localhost", "xxxx", "xxxx") or die ("Not connected");
mysql_select_db("xxxxx") or die ("no database");
$query = "SELECT xxxx_device_token FROM device_tokens";
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['xxxx_device_token'];
}
print_r($result_array);
But all I get is an empty array, what is the problem ?
your code is not correct,
try like this
mysql_connect("localhost", "xxxx", "xxxx") or die ("Not connected");
mysql_select_db("xxxxx") or die ("no database");
$query = "SELECT xxxx_device_token FROM device_tokens";
$result = mysql_query($query);
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['xxxx_device_token'];
}
$result is empty and $query is not used anywhere.
You need something like this:
$result = mysql_query($query);
http://php.net/manual/en/function.mysql-query.php
Your code is not correct. You didn't execute the query. You need to do this :
mysql_connect("localhost", "xxxx", "xxxx") or die ("Not connected");
mysql_select_db("xxxxx") or die ("no database");
$result_array = array();
$query = mysql_query("SELECT xxxx_device_token FROM device_tokens");
while($row = mysql_fetch_assoc($query))
{
$result_array[] = $row['xxxx_device_token'];
}
print_r($result_array);
But mysql_query is deprecated in new verion of PHP, so you need to do this with mysqli_query.
Try this,
$query = "SELECT xxxx_device_token FROM device_tokens";
$result = mysql_query($query);
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['xxxx_device_token'];
}
Dont use Mysql functions. It is deprecated. Please move to Mysqli (or) PDO
You have mysql_fetch_assoc($result) in the while statement, but no variable declaration for $result.
You must execute your query:
$result = mysql_query($query);
Assuming this is not a copy & paste error, you are probably missing this line:
$result = mysql_query($query);
Try not to use mysql_*, it's not recommended, use mysqli_* , ideally you should be using PDO for database connectivity.
That said you're missing this,
$result = mysql_query($query);
u have forgotten to execute your mysql query, like this
$result = mysql_query($con,"SELECT * FROM Persons");

MySQL not a valid ressource

Output:
Warning: mysql_query(): 6 is not a valid MySQL-Link resource in C:...\mysql_helper.php on line 94
$con = mysql_connect($GLOBALS['mysql_host'], $GLOBALS['mysql_username'], $GLOBALS['mysql_password']) or die(mysql_error());
$db = mysql_select_db($GLOBALS['mysql_database']) or die(mysql_error($con));
$username=sanitize_mysql($username);
$password=sanitize_mysql($password);
$email=sanitize_mysql($email);
if(check_exists("users", "username", $username) == FALSE){
$query = "INSERT INTO users VALUES('".$username."','".$password."','".$email."','".$status."','".$reg_date."','".$own_ref_id."','')";
$result = mysql_query($query,$con) or die(mysql_error($con));
return TRUE;
} else {
return FALSE;
}
mysql_close($con);
Works in every other function built like this (copy/paste)
This is check_exists
function check_exists($table,$specifier,$value)
{
$con = mysql_connect($GLOBALS['mysql_host'], $GLOBALS['mysql_username'], $GLOBALS['mysql_password']) or die(mysql_error());
$db = mysql_select_db($GLOBALS['mysql_database']) or die(mysql_error($con));
$query = "SELECT * FROM ".$table." WHERE ".$specifier." = '".$value."'";
$erg = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($erg)) {
mysql_close($con);
return TRUE;
}
mysql_close($con);
return FALSE;
}
It appears that the $con in check_exists() is in the same scope as your $con and, therefore, check_exists() first overrides (and loses) your original connection then subsequently closes its own connection when it calls mysql_close($con).
You would be better to maintain a single connection that is left open for the use of all such functions.

Categories