Jedijon Posted November 21, 2018 Share Posted November 21, 2018 $result = mysqli_query($con,"SELECT * FROM $_SESSION['datakey']"; Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 You would use an INSERT query. A SELECT query retrieves data. Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 Like this $result = mysqli_query($con,"INSERT * FROM $_SESSION['datakey']"; Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 (edited) No, not like that. You need the correct insert query syntax or there is tutorial here Edited November 21, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 I don't wan't it to be inserted into the database but use the session info as the name of the table to grave data from for the html table Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 you need to put {..} around the array element inside a string $result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 $result = mysqli_query($con,"SELECT * FROM table name"); but where the table name goes I wan't it use $_SESSION['datakey'] to get the table name Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 21, 2018 Share Posted November 21, 2018 this sounds like a bad design. 1 Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 Didn't work $result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 It will grave the info from the login SESSION Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 Define "didn't work" . That tells us nothing. What does $_SESSION['datakey'] contain? Is it the name of a table? What is your code after you get the $result? We are not telepathic! Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 $_SESSION['datakey'] contains the name of the table Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 24 minutes ago, mac_gyver said: this sounds like a bad design. I couldn't agree more. Variable tables names always rings alarm bells Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 1 minute ago, Jedijon said: $_SESSION['datakey'] contains the name of the table OK, so that leaves Define "didn't work" . That tells us nothing. What is your code after you get the $result? Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 21, 2018 Share Posted November 21, 2018 (edited) I know OP, tell us what the Real problem is you are trying to solve rather than asking about the poorly implemented attempt at solving the problem. What is the overall task at hand? Edited November 21, 2018 by benanamen Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 <?php $con=mysqli_connect("IP","Username","Password","Database name"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; echo "<table border='1'> <tr> <th></th> <th></th> <th></th> <th></th> <th></th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['column1'] . "</td>"; echo "<td>" . $row['column2'] . "</td>"; echo "<td>" . $row['column3'] . "</td>"; echo "<td>" . $row['column4'] . "</td>"; echo "<td>" . $row['column5'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 Try putting session_start() right at the top of your code. You need that to access session data Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 I have that <?php // Initialize the session session_start(); // Check if the user is logged in, if not then redirect him to login page if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){ header("location: ../login"); exit; } ?> Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 Error message Parse error: syntax error, unexpected ';', expecting ',' or ')' in /srv/disk1/2648096/www/site/test/index.php on line 20 Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 PS Before you connect to mysql, call mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); That will force it to report errors. Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 21, 2018 Share Posted November 21, 2018 Notice anything wrong here? $result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 21, 2018 Share Posted November 21, 2018 You should write query statements into a variable to make it easier to debug should you need to, as in this case. $q = "SELECT * FROM {$_SESSION['datakey']}"; echo "Query looks like: $q"; $results = mysqli_query($con, $q); Run this code and look at the actual query you created to ensure that your assumptions are correct. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2018 Share Posted November 21, 2018 3 minutes ago, Jedijon said: I have that Not in the code you showed us Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 (edited) Query looks like: SELECT * FROM datakeyWarning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in /srv/disk1/2648096/www/site/test/index.php on line 35 It shows the session data Edited November 21, 2018 by Jedijon Quote Link to comment Share on other sites More sharing options...
Jedijon Posted November 21, 2018 Author Share Posted November 21, 2018 It works Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.