Im building my website on 000webhost and with Zyro Builder and now i need some php code to get data from mysql database.
Im having this error: "Parse error: syntax error, unexpected '?' in ....." every time i want to use something like this:
echo "<br/>";
I know that is possible, so why I can't use it? :S
Thank you for your attention.
<?php
// Connect to database server
$con=mysqli_connect("mysql4.000webhost.com","a8373599_gus","******","a8373599_lolgus") or die(mysql_error($con));
if(mysqli_connect_errno($con)) {
echo "Fail to connect: " . mysqli_connect_errno();
} else {
echo "sucess!";
}
$result = mysqli_query($con, "SELECT * FROM Player");
$row = mysqli_fetch_array($result);
echo "<br/>";
echo $row['UserName']." ".$row['Playername'];
// Close the database connection
mysqli_close($con);
?>
Try using the numerical instances,
Eg:
echo $row[0]." ".$row[1];
Related
I am working on converting some PHP code from mysql to mysqli. I have created an error and am unable to understand how to fix it. Any suggestions would be greatly appreciated.
The code looks like this:
<?php
include ("admin/includes/connect.php");
$query = "select * from posts order by 1 DESC LIMIT 0,5";
$run = mysqli_query($conn["___mysqli_ston"], $query);
while ($row=mysqli_fetch_array($run)){
$post_id = $row['post_id'];
$title = $row['post_title'];
$image = $row['post_image'];
?>
The error produced is: Fatal error: Cannot use object of type mysqli as array
The error is being called out on this line:
$run = mysqli_query($conn["___mysqli_ston"], $query);
In the line above $conn is a variable from the database connect file which has this code:
<?php
// Stored the db login credentials in separate file.
require("db_info.php");
// Supressing automated warnings which could give out clues to database user name, etc.
mysqli_report(MYSQLI_REPORT_STRICT);
// Try to open a connection to a MySQL server and catch any failure with a controlled error message.
try {
$conn=mysqli_connect ('localhost', $username, $password) or die ("$dberror1");
} catch (Exception $e ) {
echo "$dberror1";
//echo "message: " . $e->message; // Not used for live production site.
exit;
}
// Try to Set the active MySQL databaseand catch any failure with a controlled error message.
try {
$db_selected = mysqli_select_db($conn, $database) or die ("$dberror2");
} catch (Exception $e ) {
echo "$dberror2";
//echo "message: " . $e->message; // Not used for live production site.
exit;
// We want to stop supressing automated warnings after the database connection is completed.
mysqli_report(MYSQLI_REPORT_OFF);
}
?>
This line
$run = mysqli_query($conn["___mysqli_ston"], $query);
should be
$run = mysqli_query($conn, $query);
If you're migrating to mysqli, you should really read these docs at least.
The proper way to use a mysqli connection:
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT id FROM test ORDER BY id ASC");
while ($row = $res->fetch_assoc()) {
echo " id = " . $row['id'] . "\n";
}
?>
you should also consider utilizing mysqli's prepared statements
I am new at cpanel and I encountered a problem. I have made a database and a database user for it in cpanel and successfully connected to them via my php code. In spite of that I have a successful connection to database but none of my queries run in the application code (while they run in the phpMyadmin!).
<?php
session_start();
$conn =new mysqli('localhost','myDBname','myDBpass','myDBuser');
if($conn)
echo "<script>alert('successful connection');</script>";
$rawresults ="SELECT * FROM `articles`";
$result = $conn->query($rawresults);
if($result->num_rows>0)
{
echo "<script>alert('dd')</script>";
$_SESSION["i"]=0;
while($results = $result->fetch_assoc())
{
setcookie("searchResult","yes");
$_SESSION["topic".$_SESSION["i"]]=$results['topic'];
$_SESSION["name".$_SESSION["i"]]=$results['fileName'];
$_SESSION["texts".$_SESSION["i"]]=$results['texts'];
$_SESSION["i"]++;
}
header('location:index.php');
}
else if($result->num_rows==0)
{
echo "<script>alert('cc')</script>";
setcookie("searchResult","yes");
header('location:index.php');
}
?>
The problem is that I permanently face: alert(cc)! While table 'articles'
contains lots of information and num_rows is a positive value.
I 'd like to mention again that connection to db has no problem and I get alert(successful connection).
Try this after connecting:
if ($conn->connect_error) {
die('Connect Error (' . $conn ->connect_errno . ') '
. $conn->connect_error);
}
I have a sqlite db file which is definitely not corrupt since I can open it with SQLiteStudio.
However, when I try to open it dynamically with PHP with the following code I found in some tutorial:
class MyDB extends SQLite3
{
function __construct()
{
$this->open('../testDB');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$sql ="SELECT * from testTable";
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) )
{
echo "ID = ". $row['id'] . "\n";
echo "NAME = ". $row['name'] ."\n\n";
}
echo "Operation done successfully\n";
$db->close();
I get the following result:
Opened database successfully
Warning: SQLite3::query() [sqlite3.query]: Unable to prepare statement: 11, database disk image is malformed in test.php on line 52
Fatal error: Call to a member function fetchArray() on a non-object in test.php on line 53
I found some threads like that one, but none of them had a definite answer.
Can somebody help me out here? Thanks in advance!
I had this same "database disk image is malformed" error and i solved it using SQLiteStudio.
Since you already have it, open the file, right click on the database and try the Vacuum option. After doing this, try the integrity check. If the result is 'OK' then your problem is solved. At least that's how i solved it. Vacuum rebuilds the entire database.
I've already the same problem with small SQLITE db.
Try to use:
$sql = "VACUUM";
$db->query($sql);
I have no idea what's going on. I usually have simple sign in pages like this done very quickly but this one isn't working and I cannot spot the error.
<?php
$con=mysql_connect("db_server","db_user","db_pass","db");
if (!$con)
{
echo "Failed to connect to MySQL: " . mysql_error();
}
$username = $_GET['username'];
$password = $_GET['password'];
$query="SELECT username FROM users ";
//$query.="WHERE `username`=".$username;
//$query.=" AND `password`=".$password;
echo "query=".$query."<br/>";
$result = mysql_query($query, $con);
echo "result=".$result."<br/>";
if($result){
$row = mysql_fetch_assoc($result);
$data = $row['username'];
echo "data=".$data;
}else{
echo "something went wrong:".mysql_error();
}
mysql_close($con);
?>
im using mysql_* instead of mysqli_* as the server im running it on is 5.2; not sure if that is relevant but I was getting an unrecognized function error originally.
There is only one entry in the database. As I said, I use the regular SQL code through phpmyadmin and i get the results i need.
Also not sure if relevant. I'm echoing $result and nothing comes out. Isnt it supposed to echo "false"?
You have a major error in your logic, for one. If there is an error connecting to MySQL, you print the error, but yet proceed to query the broken connection - you are also not selecting a database.
Also, this approach is for PHP4. Unless you are stuck in PHP4 on this project, moving into the PHP5 world would be a good idea.
I recommend looking into PDO:
http://www.php.net/manual/en/book.pdo.php
As for not getting errors, check your error_reporting and display_errors settings in your .ini
Try this one.
<?php
$con=mysql_connect("db_server","db_user","db_pass");
mysql_select_db("db");
if (!$con)
{
echo "Failed to connect to MySQL: " . mysql_error();
}
$username = $_GET['username'];
$password = $_GET['password'];
$query=mysql_query("SELECT username FROM users");
if($query){
$row = mysql_fetch_assoc($query);
$data = $row['username'];
echo $data;
}else{
echo "something went wrong:".mysql_error();
}
mysql_close($con);
?>
I am new in php, I created a page with mysql db connectivity but when the page is run it displays a blank page. If i write a echo statement before the connection statement then only echo statement is displayed and nothing else is displayed. Here is my code..
$con = mysql_connect('localhost','root','admin');
mysql_select_db('testdb',$con);
if ($con)
{
die('Connected to database!');
}
$sql = " INSERT INTO customer ([Name],[Website]) VALUES('$_POST[fname]','$_POST[lname]') ";
$result = mysql_query($sql, $con);
if(!$result)
{
echo mysql_error();
exit;
}
// close the connection
mysql_free_result($result);
mysql_close($con);
Anyone please help why this problem occurs and is there is anything wrong in the page.
Display Errors was off. I edit in the php.ini file 'display_errors' On. But still the connectivity issue is not resolved. It displays an fatal error at connectivity line statement.
fatal error: Call to undefined function mysql_connect()
$con = mysql_connect('localhost','root','admin');
mysql_select_db('testdb',$con);
if (!$con)
{
die('Not Connected to database!');
}
$sql = " INSERT INTO customer ([Name],[Website]) VALUES('$_POST[fname]','$_POST[lname]') ";
$result = mysql_query($sql, $con);
if(!$result)
{
echo mysql_error();
exit;
}
else
{
echo 'Query Success';
}
// close the connection
mysql_free_result($result);
mysql_close($con);
As you have
die('Connected to database!');
this will stop the script here, script written after it will not be executed , use instead
echo('Connected to database!');
[Name],[Website] should be replace with Name, website
your script dies everytime it success to connect to DB
change
if ($con)
{
die('Connected to database!');
}
to
if (!$con)
{
die('Connected to database!');
}